Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Capitalise first letter in editbox

Posted on 2013-06-16
4
Medium Priority
?
467 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

636 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