Tlistview对象

ljc1666ljc 2003-07-04 10:40:27
listview中的viewstyle属性为vsreport,columnclick属性为true,我想在这个事件中(listview1columnclick(sender:tobject;columnclick:tlistcolumn))进行排序,怎么写这里的代码啊?
...全文
88 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljl0206 2003-09-06
  • 打赏
  • 举报
回复
首先声明两个全局变量来保存被单据的列的索引号和当前索引状态(ASC或DESC)
在你的单元接口段的private下声明:
columntosort:integer;
isascsort:boolean;

在ListView的ColumnClick事件里给columntosort赋值(被单击列的索引号),调用AlphaSort:
procedure TForm1.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
begin
isascsort:=not isascsort;
columntosort:=column.Index;
(sender as tcustomlistview).AlphaSort;
end;

然后在ListView的Compare事件里写排序代码:

procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
Data: Integer; var Compare: Integer);
var
xx:integer;
begin
if columntosort=0 then//按标题列排序;
if isascsort then
compare:=comparetext(item1.Caption,item2.Caption)
else
compare:=comparetext(item2.Caption,item1.Caption)
else//按SubItems排序
begin
xx:=columntosort-1;
if isascsort then
compare:=comparetext(item1.SubItems[xx],item2.SubItems[xx])
else
compare:=comparetext(item2.SubItems[xx],item1.SubItems[xx]);
end;
end;

以上须注意排序原理,相应函数可查DELPHI帮助.
dickeybird888 2003-07-04
  • 打赏
  • 举报
回复
用回调函数的第三个参数传入对各个Column的排序.

function SortByString(Item1, Item2: TListItem; ColumnIndex: integer): integer; stdcall;
begin
if ColumnIndex = 0 then
Result := CompareText(Item1.Caption,Item2.Caption);
else
if Item1.SubItems[ColumnIndex-1] <= Item2.SubItems[ColumnIndex-1]
then Result := -1
else Result := 1;
end;

使用
procedure TSySortListView.ColumnClick(Sender: TObject; Column: TListColumn);
begin
CustomSort(@SortByString,Column.Index);
end;
ljc1666ljc 2003-07-04
  • 打赏
  • 举报
回复
Means_pan(酷鱼)兄弟,请问一下你的第一个程序是dll程序吗?他引用了什么单元啊我的编译不了啊能详细一点吗?
huayuxing 2003-07-04
  • 打赏
  • 举报
回复
procedure TFra_Change.LvSourceColumnClick(Sender: TObject;
Column: TListColumn);
begin
ColumntoSort:=column.Index;
(Sender as TCustomListView).AlphaSort;
end;

procedure TFra_Change.LvSourceCompare(Sender: TObject; Item1,
Item2: TListItem; Data: Integer; var Compare: Integer);
var
ix:integer;
begin
if ColumntoSort =0 then
Compare:=CompareText(Item1.Caption,Item2.Caption)
else
begin
ix:=ColumntoSort-1;
Compare:=CompareText(Item1.SubItems[ix],Item2.SubItems[ix]);
end;

end;
Means_pan 2003-07-04
  • 打赏
  • 举报
回复


function CustomSortProc( Item1, Item2 : TListItem; lParam : LongInt ) : Integer; stdcall;

begin

if lParam >= 0 then //lParam中保存的是SubItem的Index

begin

result := -CompareText(Item1.SubItems.Strings[lParam],

Item1.SubItems.Strings[lParam] );

end else

result := 0;

end;

在ListView的ColumnClick事件响应方法中输入

CustomSort(@CustomSortProc, Column.Index );

 



Currency 2003-07-04
  • 打赏
  • 举报
回复
procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn);

begin
ColumnToSort := Column.Index;
(Sender as TCustomListView).AlphaSort;
end;
Delphi 帮助里有例子,

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧