Delphi7的ListView点击列头实现排序

 时间:2024-10-13 17:16:01

1、打开Delphi7集成开发环境,在默认工程的Form1窗体放一个ListView1控件,并右键ListView1控件,打开Column editor对话框,新建三个列,Capiton分别为姓名,班级,成绩。

Delphi7的ListView点击列头实现排序
Delphi7的ListView点击列头实现排序

2、将ListView1的ViewStyle设为vsReport,GoldLines显示设为True。

Delphi7的ListView点击列头实现排序
Delphi7的ListView点击列头实现排序

3、在Form1窗体的OnShow事件响应方法中,为ListView1添加数据,代码如下:procedure TForm1.僻棍募暖FormShow(Sender: TObject);var item:TListItem;begin item := ListView1.items.add(); item.Caption := '小王'; item.SubItems.Add('一班'); item.SubItems.Add('80'); item := ListView1.items.add(); item.Caption := '小赵'; item.SubItems.Add('一班'); item.SubItems.Add('60'); item := ListView1.items.add(); item.Caption := '小李'; item.SubItems.Add('一班'); item.SubItems.Add('70'); item := ListView1.items.add(); item.Caption := '小韩'; item.SubItems.Add('一班'); item.SubItems.Add('80');end;

Delphi7的ListView点击列头实现排序

4、F9运行程序,可以看到数据被正常添加并显示了

Delphi7的ListView点击列头实现排序

5、接着在Unit.pas的var Form1:哌囿亡噱TForm1下加一个全局变量 :m_bSort: boolean=false;在程序implement部分定义如下函数:fu荏鱿胫协nction CustomSortProc(Item1, Item2: TListItem; Index: integer): integer;stdcall;varc1,c2 :string;begin if index<>0 then begin c1:= Item1.SubItems.Strings[index-1]; c2:= Item2.SubItems.Strings[index-1]; if m_bSort then begin Result := CompareText(c1,c2); end else begin Result := -CompareText(c1,c2); end; end else begin if m_bSort then begin Result := CompareText(Item1.Caption,Item2.Caption); end else begin Result := -CompareText(Item1.Caption,Item2.Caption); end; end;end;这个是ListView1进行比较两行数据大小的回调函数,我们可以自己定义,然后在ListView1的OnColumnClick事件中进行调用。

6、在ListView1的OnColumnClick事件中写如下代码:procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn);begin ListView1.CustomSort(@CustomSortProc,Column.Index); m_bSort := not m_bSort;end;

Delphi7的ListView点击列头实现排序

7、F9运行程序,点击列头就能看到ListView1的排序了

Delphi7的ListView点击列头实现排序
Delphi7的ListView点击列头实现排序
  • 备份Eclipse或MyEclipse中workspace的方法介绍
  • wamp安装完成后访问localhost出现404错误
  • 如何将字典添加到数组
  • 教大家使用Axure制作单选效果的表格
  • 如何手动添加android中xml布局文件的颜色
  • 热门搜索
    国庆节手抄报。 51手抄报 我爱读书手抄报的内容 节约用水手抄报内容 中秋节手抄报。 关于健康的英语手抄报 成长足迹手抄报 五一的手抄报 三年级手抄报图片 法律手抄报图片