?
Solved

Capitalise first letter in editbox

Posted on 2013-06-16
4
Medium Priority
?
457 Views
Last Modified: 2013-06-18
When I enter a word into an editbox. I want the first letter to be automatically capitalised:

test will become Test

I am using Delphi 6.  Thanks for your help.
0
Comment
Question by:rincewind666
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 30

Accepted Solution

by:
Randy Downs earned 2000 total points
ID: 39251203
Try this

http://delphi.about.com/cs/adptips1999/a/bltip0799_2.htm

Add the following code to the OnKeyPress event for the Edit1 component:

~~~~~~~~~~~~~~~~~~~~~~~~~
with Sender as TEdit do
    if (SelStart = 0) or 
       (Text[SelStart] = ' ') then
          if Key in ['a'..'z'] then 
             Key := UpCase(Key) ;

Open in new window

0
 
LVL 24

Expert Comment

by:jimyX
ID: 39251232
In case you want to capitalize when pasting from clipboard as well as when typing then you can use OnChange event of the TEdit:
procedure TForm1.Edit1Change(Sender: TObject);
var
  i:integer;
begin
   i := Edit1.SelStart;
  if (Edit1.Text <> '') and(Edit1.Text[1] in ['a'..'z']) then
    begin
      Edit1.Text := StringReplace(Edit1.Text, Edit1.Text[1], UpperCase(Edit1.Text[1]), []);
       Edit1.SelStart := i;
    end;
end;

Open in new window

0
 
LVL 24

Expert Comment

by:jimyX
ID: 39251254
And if you want to keep the first char only capital even if the cursor has been placed at the beginning of the text and a new char was entered then the previously capitalized char would be lowered, then:
procedure TForm1.Edit1Change(Sender: TObject);
var
  i:integer;
begin
    if (Length(Edit1.Text) > 1) and ((Edit1.SelStart = 1)) and(Edit1.Text[2] in ['A'..'Z'])  then
      begin
        Edit1.Text := StringReplace(Edit1.Text, Edit1.Text[2], LowerCase(Edit1.Text[2]), []);
      end;

  if (Edit1.Text <> '') and(Edit1.Text[1] in ['a'..'z']) then
    begin
      i := Edit1.SelStart;
      Edit1.Text := StringReplace(Edit1.Text, Edit1.Text[1], UpperCase(Edit1.Text[1]), []);
      Edit1.SelStart := i;
    end;
end;

Open in new window

0
 

Author Closing Comment

by:rincewind666
ID: 39256112
Many thanks for your help.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month15 days, left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question