Lazarus

Programming => LCL => Topic started by: egsuh on July 22, 2021, 02:08:03 pm

Title: [SOLVED] TCombobox texts are not cleared
Post by: egsuh on July 22, 2021, 02:08:03 pm
I populate TCombobox is populated, and select an item, and then I clear all items. But the text of selected item is still live.

I have to set ItemIndex to -1 before clearing items to remove text as follows (Lazarus 2.0.10,  Windows 10).

Combobox1.ItemIndex := -1;
Combobox1.Items.Clear;
Title: Re: TCombobox texts are not cleared
Post by: zoltanleo on July 22, 2021, 02:33:54 pm
@egsuh

Try
Code: Pascal  [Select][+][-]
  1. Combobox1.Clear;
  2.  
Title: Re: TCombobox texts are not cleared
Post by: lucamar on July 22, 2021, 07:53:33 pm
The result of:
Code: Pascal  [Select][+][-]
  1. Combobox1.Items.Clear;
depends on the Style of the ComboBox.

For the ones that allow you to set the Text to anything other than the items in the list (like the default csDrowpDown or csSimple) it will just clear the items but, logically enough, the Text will remain as is. If instead you set Style to, say, csDropDownList clearing the items will also clear the Text since there is no item from which to get it.

That's one reason why there exists TComboBox.Clear; whatever the Style it'll clear both the Items and the Text by doing:
Code: Pascal  [Select][+][-]
  1. procedure TCustomComboBox.Clear;
  2. begin
  3.   Items.Clear;
  4.   Text:='';
  5. end;
which looks like someone came up against your same problem and added that as a "workaround" :D
Title: Re: TCombobox texts are not cleared
Post by: wp on July 22, 2021, 11:45:39 pm
Lazarus behaves like Delphi:
- Combobox.Items.Clear clears the dropdown list but keeps the Text (when Style = csDropdown),
- Combobox.Clear clears both.
Title: Re: TCombobox texts are not cleared
Post by: lucamar on July 23, 2021, 12:54:11 am
Actually, the Clear should work all around..

Well, Combobox.Clear does it; Items.Clear, logically enough, only clears the strings in Items. Only when Combobox.Style is csDropDownList (or the equivalent "OwnerDraw" modes) the combo is notified since it means that since ItemIndex is no longer valid there is nothing to show as Text.

Quote
Just checked the trunk in Windows, it clears it all regardless of combo style

As it should if you're talking about Combobox.Clear; otherwise, if you mean Items.Clear, it's clearly a bug.
Title: Re: TCombobox texts are not cleared
Post by: egsuh on July 23, 2021, 03:35:38 am
Combobox1.Clear;

Clears all. Thank you for all your comments.
TinyPortal © 2005-2018