2005. december 5., hétfő

How to make the first letter of a user-entered word in a TComboBox always uppercase


Problem/Question/Abstract:

Does anyone have a routine for uppercasing only the first letter of each word the user typed in an edit control - especially a combo box. I've experimented with the mask property for the dataset field but am not having any luck.

Answer:

procedure TForm1.ComboBox1Change(Sender: TObject);
var
  s, TempString: string;
  Cnt, temp: integer;
begin
  s := ComboBox1.Text;
  temp := ComboBox1.SelStart;
  if s <> '' then
  begin
    for Cnt := 1 to Length(s) do
    begin
      if (Cnt = 1) or (s[Cnt - 1] in [' ', ', ', ' ;']) then {or others here}
      begin
        TempString := s[Cnt];
        s[Cnt] := AnsiUpperCase(TempString)[1];
      end;
    end;
    ComboBox1.Text := s;
  end;
  ComboBox1.SelStart := temp;
end;

I strongly recommend to use the AnsiUpperCase function, if there's any possibility that non-English words may ever be entered into your application.

Nincsenek megjegyzések:

Megjegyzés küldése