![]() |
Delphi-Version: 7
CheckListBox auf- und absteigend sortieren
Hallo.
Eine CheckListBox kann man ja ganz einfach mit
Code:
aufsteigend sortieren.
.Sorted := True;
Ich möchte aber per ButtonClick einstellen können, ob die CLB auf- oder absteigend sortiert werden soll. Muss ich dafür schon einen Sortieralgorithmus benutzen, oder gibt es da einen Einzeiler von Delphi? Gruß |
AW: CheckListBox auf- und absteigend sortieren
Sorted sortiert IIRC immer aufsteigend, Du wirst also CustomSort verwenden müssen.
|
AW: CheckListBox auf- und absteigend sortieren
Sorted setzt das CBS_SORT in den Window-Styles ... was genau damit los ist, steht im MSDN.
|
AW: CheckListBox auf- und absteigend sortieren
Zitat:
|
AW: CheckListBox auf- und absteigend sortieren
Zur Not in eine TStringlist kopieren, diese mit CustomSort sortieren und wieder zurückkopieren. Die Checked-Eigenschaft kannst Du dabei mit einem üblen Cast als Object hinterlegen, damit sie nicht verloren geht.
[edit] CBS_SORT? Nicht LBS_SORT? [/edit] [edit2] Oder ohne TStringlist:
Delphi-Quellcode:
Beispielaufruf:
type
TSortFunc = function(List: TStrings; Index1, Index2: Integer): Integer; function CompareASC(List: TStrings; Index1, Index2: Integer): Integer; begin Result := StrComp(PChar(List[Index1]), PChar(List[Index2])); end; function CompareDESC(List: TStrings; Index1, Index2: Integer): Integer; begin Result := - CompareASC(List, Index1, Index2); end; procedure QuickSort(L, R: Integer; List: TStrings; SCompare: TSortFunc); var I, J, P: Integer; begin repeat I := L; J := R; P := (L + R) shr 1; repeat while SCompare(List, I, P) < 0 do Inc(I); while SCompare(List, J, P) > 0 do Dec(J); if I <= J then begin List.Exchange(I, J); if P = I then P := J else if P = J then P := I; Inc(I); Dec(J); end; until I > J; if L < J then QuickSort(L, J, List, SCompare); L := I; until I >= R; end;
Delphi-Quellcode:
[/edit2]
procedure TForm1.Button1Click(Sender: TObject);
begin CheckListBox1.Items.BeginUpdate; try QuickSort(0, CheckListBox1.Items.Count - 1, CheckListBox1.Items, CompareASC); finally CheckListBox1.Items.EndUpdate; end; end; |
AW: CheckListBox auf- und absteigend sortieren
Zitat:
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 01:12 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz