TEdit's OnKeyPress - How to capitalize first character of each word ??

DelphiRulez
DelphiRulez used Ask the Experts™
on
How to capitalize first character of each word in a TEdit's OnKeyPress ??
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Hi, take a look at this, it has what you need...

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

Commented:
Another one, maybe faster...

http://www.delphi3000.com/article.asp?ID=3999

Author

Commented:
How to capitalize first character of each word in a TEdit's OnKeyPress ??
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.


unit Unit35;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm35 = class(TForm)
    Memo1: TMemo;
    procedure Memo1KeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form35: TForm35;
  LastChar: Char;

implementation

{$R *.dfm}

procedure TForm35.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
  If LastChar = #32 Then
    begin
    Key := Uppercase(Key)[1];
  end;
  LastChar := Key;
end;

end.

Open in new window

Commented:
@ThievenSix

Your code will miss Capitalizing the first word on a phrase as there is no space before it...

If should be like this

procedure TForm35.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
  If (LastChar = #32) or (Length(Memo1.Text) = 1) Then
   begin
    Key := Uppercase(Key)[1];
   end;
  LastChar := Key;
end;
Well if you're going that far, there are more ways to cover it. Such as the enter key, or pasting. I just gave a rough example.
Senior developer, analyst and customer assistance
Commented:
My simple (working) sample

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if TEdit(Sender).SelLength = Length(TEdit(Sender).Text) then
    Key := Upcase(Key)
  else if TEdit(Sender).SelStart <= Length(TEdit(Sender).Text) then
    if TEdit(Sender).Text[TEdit(Sender).SelStart] = ' ' then
      Key := Upcase(Key);
end;

Author

Commented:
NICE!  Just what i was looking for.....THANKS!

Shane

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial