Solved

Capitalise first letter in editbox

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now