Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting a CheckListBox 2

Status
Not open for further replies.

RyanEK

Programmer
Apr 30, 2001
323
AU
Hi,

What's the best way to sort a CheckListBox so that the checked items appear on top of the list first?

Thanks for your help
Ry
 
Code:
var a: integer;
begin
 for a:= 0 to CheckListBox1.items.count-1 do
  if CheckListBox1.Checked[a] then CheckListBox1.Items.Move(a, 0)
end;
[\code]

to sort by checked and in alphabetical order is another question.

Aaron
 
Or better...

Tested: Sorts TCheckListBox items so checked items are placed on the top. Does not have regard to order of the items themselves, just moves the checked items to the top.

Code:
var
  i, j: integer;
begin
  i := 0;
  while (i < CheckListBox1.Items.Count-1) do
    begin
      if CheckListBox1.Checked[i] = true then
        inc(i)
      else
        break;
    end;
  j := i;
  while (j <= CheckListBox1.Items.Count-1) do
    begin
      if CheckListBox1.Checked[j] = true then
        begin
          CheckListBox1.Items.Exchange(j, i);
          CheckListBox1.Checked[i] := true;
          CheckListBox1.Checked[j] := false;
          inc(i);
        end;
      inc(j);
    end;
end;
 
Thanks guys, I appreciate it. Both solutions work well :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top