Solved

Sorting a Listview by colums

Posted on 1998-07-17
10
299 Views
Last Modified: 2008-02-01
how can I sort a listbox by clicking a column? I've tried changing the columsclick event, but unsucessfuly. How can I do it. Thanks.
0
Comment
Question by:jpcs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 5

Expert Comment

by:mayhew
ID: 1358131
Is it that you want to sort the whole listbox by clicking on any column?

If so, in the ListBox1OnClick method:
  ListBox1.Sorted := true;

If I am misunderstanding your question, please rephrase it and we'll take another look.  :)
0
 

Accepted Solution

by:
bome earned 20 total points
ID: 1358132
I assume that your listbox is a TListView. Let's call it ListView:
Create handlers for OnColumnClick and OnCompare:

procedure TForm1.ListViewColumnClick(Sender: TObject;
  Column: TListColumn);
begin
 ListView.CustomSort(nil,Column.index);
end;


procedure TForm1.ListViewCompare(Sender: TObject; Item1,
  Item2: TListItem; Data: Integer; var Compare: Integer);
begin
 if (Item1=nil) and (Item2=nil) then
  Compare:=0
 else
 if (Item1=nil) and (Item2<>nil) then
  Compare:=-1
 else
 if (Item1<>nil) and (Item2=nil) then
  Compare:=1
 else
 if Data=0 then
  Compare:=ComPareStr(Item1.Caption,Item2.Caption)
 else
 // sanity check
 if (Data<=Item1.SubItems.Count) and (Data<=Item2.SubItems.Count) then
  Compare:=ComPareStr(Item1.SubItems[Data-1],Item2.SubItems[Data-1]);
 // sort by name as second criteria
 if (Compare=0) and (Data<>0) then
  ListViewCompare(sender,Item1,Item2,0,Compare);
end;

These procedures sort alphanumerically the respective column when you click on the column header.

bome
0
 
LVL 1

Author Comment

by:jpcs
ID: 1358133
Thanks for the grat answer bome. Gee, It's was far more dificult than I thought.

There are some tinny little problems that I would thank you if you help me :

1. If I click a column, it does now sort the items (from a to z), but if I click it again, it should resort from z to a. How can I do it?

2. Why is "T" (upcase) before "a" (lowercase) and why is "2" after "10"? How can I solve this? (I don't want to use "02" instead "2".


Thank you very much for your answer.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:jpcs
ID: 1358134
Sorry everybody, but in fact it is a ListView control, not a ListBox. Sorry.
0
 

Expert Comment

by:bome
ID: 1358135
Hi,
no problem.
1. add 2 variables to your form: e.g.
sortIndex: Integer;
reversesort:boolean;

in the ColumnsClick handler you add at the beginning:
if sortIndex=Column.index then
 reverseSort:=not reverseSort;
sortIndex:=Column.Index;

Replace the last 2 lines in the ListViewCompare procedure with these:
if (Compare=0) and (Data<>0) then
      ListViewCompare(sender,Item1,Item2,0,Compare)
else
if reverseSort then
 Compare:=-Compare;

2. To have case insensitive sort, replace all CompareStr with CompareText.

That's it, thank you for the points !
bome
0
 
LVL 1

Author Comment

by:jpcs
ID: 1358136
Thanks.
0
 
LVL 1

Author Comment

by:jpcs
ID: 1358137
Just another thing : Can you tell me how to avoid the user to resize any column header?

Thanks
0
 

Expert Comment

by:bome
ID: 1358138
no, I don't. I think this is intended by Microsoft - try Ctrl+'+' - it resizes automatically. So you would have to care about some events. The only thing I can imagine is to access the listview directly with windows API - I don't have the time at the moment to check this out. The names of these "functions" start all with "ListView_"...

Another thing why this could get a problem: Column width is in pixels - but what if the user changes the standard font size ? I haven't tried, but very probably the column titles change size, too, so eventually the user wouldn't be able to read the column title when he has big fonts.

bome
0
 

Expert Comment

by:bome
ID: 1358139
no, I don't. I think this is intended by Microsoft - try Ctrl+'+' - it resizes automatically. So you would have to care about some events. The only thing I can imagine is to access the listview directly with windows API - I don't have the time at the moment to check this out. The names of these "functions" start all with "ListView_"...

Another thing why this could get a problem: Column width is in pixels - but what if the user changes the standard font size ? I haven't tried, but very probably the column titles change size, too, so eventually the user wouldn't be able to read the column title when he has big fonts.

bome
0
 

Expert Comment

by:ibrobar
ID: 10632280
Dear Bome

I want to thank u for the great solution and for your help
I am having a problem with a listview too let me explain it
I have a listview with 6 static columns.
I have a popup menu attached with the listview.From the popup menu i can call listbox showing table fields.
what I need to do is to drag and drop from that listbox to the listview and to get the effects due to the drag and drop event.
here is my code: {I applied this code on listviewDragdrop procedure}

var I:integer;
    NewColumn: TListColumn;
    ListItem:TListItem;
    S:String;
begin
if (sender=listview1) and (source=form10.ListBox1) then
For i := 0 to (Form10.ListBox1.Items.Count - 1) do begin
   if Form10.ListBox1.Selected[i] then begin
      NewColumn:= listview1.Columns.add;
      NewColumn.Caption :=Form10.listbox1.items.Strings[i];
      NewColumn.Width:=100;
      S:=Form10.listbox1.items.Strings[i];
   end;
end;
DataModule1.Query13.Open;
If DataModule1.Query13.RecordCount<>0 then begin
     ListView1.Items.Clear;
     DataModule1.Query13.First;
    while not DataModule1.QUERY13.EOF do begin
       listitem:=Form2.listview1.Items.Add;
       listitem.Caption:=DataModule1.Query13['TITLE'];
       listitem.SubItems.Add(DataModule1.Query13['atitle']);
       listitem.SubItems.Add(DataModule1.Query13['borrower']);
       listitem.SubItems.Add(DataModule1.Query13['overdue']);
       listitem.SubItems.Add(DataModule1.Query13['cdate']);
       listitem.SubItems.Add(DataModule1.Query13['bdepartment']);
       Listitem.SubItems.Add(DataModule1.Query13[s]);
       listitem.ImageIndex:=0;
       DataModule1.QUERY13.Next;
   end;
end

as you can see from my code that I am adding new columns with out having any problems and i can add one subitems prefectly but when I want to drag and drop another item from the listbox the new subitems is coming over the old one cause you know it's coming at the end of the listview items.
I tried the insert procedure it didn't work , I tried many loops but I am still having the same problem
so can I get your help here.
one more thing how can  save the new layout of the listview so i can ask get again when I want the same layout.

thanks a lot

regards
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tidtcpserver connection lost handle 2 111
Adoquery sql  left join does not work 25 106
Delphi and Access based Enumeration 9 78
Delphi, TImage, Png 2 32
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question