Recent

Author Topic: re-sorting combobox / listbox  (Read 7035 times)

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
re-sorting combobox / listbox
« on: April 01, 2015, 09:17:58 pm »
Hi there, I am about three weeks under way with object pascal and really enjoy Lazarus 1.4RC2 on Debian 7.

The one thing I really do not understand is why a combobox and listbox do not appear to have a single command to invoke a re-sort of their items.

cbCategory.Sorted := True  helps when adding items to the list, but it does not help when renaming an item:

cbCategory.Items.Strings[cbCategory.ItemIndex] := Category.Name

Eventually I came up with what looks like a workaround:

Code: [Select]
cbCategory.Items.Strings[cbCategory.ItemIndex] := Category.Name
cbCategory.Sorted := false;
cbCategory.Sorted := true;

Is this the way to go, or am I missing something?

keep it simple

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: re-sorting combobox / listbox
« Reply #1 on: April 01, 2015, 10:29:02 pm »
Note that TStrings is an abstract class but property Items is created as TStringList. Therefore you can use its method:
Code: [Select]
TStringList(cbCategory.Items).Sort;
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: re-sorting combobox / listbox
« Reply #2 on: April 01, 2015, 10:39:44 pm »
Thanks Blaazen. That's really helpful!
keep it simple

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: re-sorting combobox / listbox
« Reply #3 on: April 01, 2015, 10:48:05 pm »
I have a SIGSEGV with that line of code.

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: re-sorting combobox / listbox
« Reply #4 on: April 01, 2015, 10:49:48 pm »
I don't (on Linux).
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: re-sorting combobox / listbox
« Reply #5 on: April 01, 2015, 10:53:11 pm »
I have a SIGSEGV with that line of code.
ofcourse you do. In windows the items is stringlist based only until the win32 widgetset replaces it. have a look on the prrivate method  TCustomCombox.updateSorted for the correct way to call sort.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: re-sorting combobox / listbox
« Reply #6 on: April 01, 2015, 10:55:50 pm »
It is private.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: re-sorting combobox / listbox
« Reply #7 on: April 01, 2015, 11:31:13 pm »
here have fun.
Code: [Select]

uses WSStdCtrls;

procedure UpdateSorted(const aBox:TComboBox);
var
  lText: string;
  lIndex: integer;
begin
  if aBox.HandleAllocated then
    TWSCustomComboBoxClass(aBox.WidgetSetClass).Sort(aBox, aBox.Items, aBox.Sorted)
  else if aBox.Items is TStringList then
  begin
    // remember text
    lText := aBox.Text;
    TStringList(aBox.Items).Sorted := aBox.Sorted;
    lIndex := aBox.Items.IndexOf(lText);
    if lIndex >= 0 then
      aBox.ItemIndex := lIndex;
  end;
end;       
« Last Edit: April 01, 2015, 11:34:09 pm by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: re-sorting combobox / listbox
« Reply #8 on: April 01, 2015, 11:40:55 pm »
Thanks taazz, I will save that code for later use.
keep it simple

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: re-sorting combobox / listbox
« Reply #9 on: April 05, 2015, 02:45:42 pm »
Strange, while continuing my project, I now also get a SIGSEGV with this line:
Code: [Select]
TStringList(cbCategory.Items).Sort;
No code change seems to relate directly to it, so I better stick to the workaround, which seems the best reliable approach so far. However, I still think that a list entry's name change should also trigger the built-in sort routine. Or the sort routine should be made accessible.
keep it simple

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: re-sorting combobox / listbox
« Reply #10 on: April 05, 2015, 03:21:45 pm »
I think the best is to keep using

Sorted := True / Sorted := False

as you already do.

 

TinyPortal © 2005-2018