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
Solved

Capitalise first letter in editbox

Posted on 2013-06-16
4
452 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
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
Randy Downs earned 500 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert Jpg /PNG To GIF 5 150
LAN or WAN ? 11 95
Graphics32 under Delphi 10.1 Berlin 2 102
Delphi Ado Query To Converts Joined Columns Into Rows Based on Primary Key 4 69
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

839 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