Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
A custom sort a TCheckListBox Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
16-May-03
Category
VCL-General
Language
Delphi 2.x
Views
177
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

I created a component TMyCheckListBox which inherited from TCheckListBox, and I 
want to sort the list of items in numeric not alphabetic order. Although I set the 
Sorted property to True, I cannot sort it in the way I want. I need to have a 
numeric sort with the Sorted property set to True.

Answer:

The Sorted property calls the built-in capability of the underlying windows control 
to sort itself alphabetically. In case you need to perform a custom sorting, just 
turn the Sorted property off, and do the sorting by using TStringList CustomSort 
method. Below is the sequence of possible steps

Assign listbox's items to the string list. Perform custom sorting by calling the 
CustomSort method. You should pass a function that compares two strings in the 
string list as parameter (see example below for details). Move items back to the 
listbox. Here's an example. It resorts the list's content in custom order:

1   {This function sorts items in the list}
2   
3   function CompareStrings(List: TStringList; Index1, Index2: Integer): Integer;
4   var
5     XInt1, XInt2: integer;
6   begin
7     try
8       XInt1 := strToInt(List[Index1]);
9       XInt2 := strToInt(List[Index2]);
10    except
11      XInt1 := 0;
12      XInt2 := 0;
13    end;
14    Result: = XInt1 - XInt2;
15  end;
16  
17  procedure TForm1.SpeedButton5Click(Sender: TObject);
18  var
19    XList: TStringList;
20  begin
21    XList := TStringList.Create;
22    CheckListBox1.Items.BeginUpdate;
23    try
24      XList.Assign(CheckListBox1.Items);
25      XList.CustomSort(CompareStrings);
26      CheckListBox1.Items.Assign(XList);
27    finally
28      XList.Free;
29      CheckListBox1.Items.EndUpdate;
30    end;
31  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC