Recent

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

egsuh

  • Hero Member
  • *****
  • Posts: 828
[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: 353
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/Darwin Cocoa (Big Sur):
Lazarus x32/x64 2.3(trunk); FPC 3.3.1 (trunk), FireBird 3.0.7

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.

jamie

  • Hero Member
  • *****
  • Posts: 4837
Re: TCombobox texts are not cleared
« Reply #3 on: July 22, 2021, 11:23:06 pm »
Actually, the Clear should work all around..

I will look into this a little deeper and see what the Windows API does with a clear but I am pretty sure it clears the list and text in a single shot..

 I could be wrong, it wouldn't be the first time in my life..


"I am never wrong, once I thought I was but turned out to be a mistake" :o


P.S.

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


 maybe a widget issue on other targets




« Last Edit: July 22, 2021, 11:31:03 pm by jamie »
The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 8732
Re: TCombobox texts are not cleared
« Reply #4 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.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

lucamar

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

jamie

  • Hero Member
  • *****
  • Posts: 4837
Re: TCombobox texts are not cleared
« Reply #6 on: July 23, 2021, 01:00:37 am »
keeping with Delphi bugs is usually not on the top of my list but the CLEAR does behave exactly as documented via Delphi style..

 I can't say much for the items.Clear, I guess that is a matter of how you would like it to work.

 The items is nothing more than a Tstringlist being emulated to a point so while the BeginUpdate is active there, making changes in that list does not update the widget list until later..

 This makes it for faster updates..

 putting that aside I don't see any issues here on the windows side.
The only true wisdom is knowing you know nothing

egsuh

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

Clears all. Thank you for all your comments.

 

TinyPortal © 2005-2018