Mirror

Automatically capitalize the first letter of every word when typing in the field of a TStringGrid (Views: 102)


Problem/Question/Abstract:

How to automatically capitalize the first letter of every word when typing in the field of a TStringGrid

Answer:

Solve 1:

procedure TForm1.StringGrid1KeyPress(Sender: TObject; var Key: Char);
var
  s: string;
  c: Byte;
begin
  with StringGrid1 do
    s := Cells[Col, Row];
  if Length(s) = 0 then
  begin
    if Key in ['a'..'z'] then
    begin
      c := Ord(Key) - 32;
      Key := Chr(c);
    end;
    exit;
  end;
  if s[Length(s)] = ' ' then
    if Key in ['a'..'z'] then
    begin
      c := Ord(Key) - 32;
      Key := Chr(c);
    end;
end;


Answer 2:

In an onKeyPress event, do this:

if length(field.text) = 0 then
  key := upCase(key);

<< Back to main page