Recent

Author Topic: [SOLVED] TCombobox texts are not cleared  (Read 5091 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1291
[SOLVED] TCombobox texts are not cleared
« 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;
« Last Edit: July 23, 2021, 03:35:53 am by egsuh »

zoltanleo

  • Sr. Member
  • ****
  • Posts: 488
Re: TCombobox texts are not cleared
« Reply #1 on: July 22, 2021, 02:33:54 pm »
@egsuh

Try
Code: Pascal  [Select][+][-]
  1. Combobox1.Clear;
  2.  
Win10 LTSC x64/Deb 11 amd64(gtk2/qt5)/Darwin Cocoa (Monterey):
Lazarus x32/x64 2.3(trunk); FPC 3.3.1 (trunk), FireBird 3.0.10; IBX by TonyW

Sorry for my bad English, I'm using translator ;)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: TCombobox texts are not cleared
« Reply #2 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
« Last Edit: July 22, 2021, 07:55:34 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: TCombobox texts are not cleared
« Reply #3 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.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: TCombobox texts are not cleared
« Reply #4 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.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

egsuh

  • Hero Member
  • *****
  • Posts: 1291
Re: TCombobox texts are not cleared
« Reply #5 on: July 23, 2021, 03:35:38 am »
Combobox1.Clear;

Clears all. Thank you for all your comments.

 

TinyPortal © 2005-2018