0
votes

in delphi, can add combobox items with different case(in my example uppercase). `

ComboBox1.Items.Add('SYDNEY');
ComboBox1.Items.Add('MOSCOW');
ComboBox1.Items.Add('BERLIN');
ComboBox1.Items.Add('BERN');
ComboBox1.Items.Add('PARIS');

`

combobox shows like that

but when try search, combobox text differs from items case type

and will be inserted into database as 'bERLIN' instead of 'BERLIN'.

UniQuery1.SQL.Add('INSERT INTO login (id,user) values (10,'''+ComboBox1.text+''')');

Is there way to automatically change text to actual items case

1
Please add to your q the code you use to transfer the combobox text to your database field. See Minimal, Reproducible ExampleMartynA
UniQuery1.SQL.Add('INSERT INTO login (id,user) values (10,'''+ComboBox1.text+''')');Mamed Aliyev
UpperCase(ComboBox1.Text)Delphi Coder
I edited your question to include the code from your comment. Please always edit (see edit button on the left) added info into your question.Tom Brunberg

1 Answers

3
votes

In your example you have all city names in uppercase. Thus you can indeed use the UpperCase() function as suggested in a comment.

However, a more general solution would be to instead check if ItemIndex <> -1 indicating that an item indeed is selected and then use Items[ItemIndex] to store in the db instead of the Text property.


Edit after comment:

To update the Text property as well as selection, try this:

procedure TForm20.ComboBox1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  ss, sl: integer;
  SelectedItemString: string;
begin
  with ComboBox1 do
  begin
    ss := SelStart;
    sl := SelLength;
    if ItemIndex <> -1 then
      SelectedItemString := Items[ItemIndex]
    else
      SelectedItemString := Text;
    Text := SelectedItemString;
    SelStart := ss;
    SelLength := sl;
  end;
  Key := 0;
end;