Solved

Capitalise first letter in editbox

Posted on 2013-06-16
4
451 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 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