ListView排序的问题

fei19790920 2003-04-20 04:08:43
怎么才能实现像资源管理器那样的排序功能,能够倒序和顺序!!
...全文
32 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
fengyu8498 2003-06-08
  • 打赏
  • 举报
回复
回一楼的:
十分感谢你的代码,我刚刚COPY去用了下,但有一个地方该改动一下:

procedure TForm1.FormCreate(Sender: TObject);
begin
ColumnToSort := -1;//此处的 (-1) 改为 (1)
IsAscSort := True;
end;

如果不改,就没法给例表动态添加项目了,如果用
with listview.add do Caption:='Test';
两次就会出现一个错误:
EStringListError,List index out of bounds.(-2)
你这个错误还真的难找,找了我一个下午。

http://expert.csdn.net/Expert/TopicView1.asp?id=1891201
u2m 2003-04-25
  • 打赏
  • 举报
回复
function ListViewRowSort(mListView: TListView; mByte: integer; mDesc: Boolean =
False): Boolean;
function ListViewRowSwap(mListView: TListView; mFromRow, mToRow: Integer):
Boolean;
var
S: string;
begin
Result := False;
if (mToRow = mFromRow) then
Exit;
if not Assigned(mListView) then
Exit;
if (mFromRow < 0) or (mFromRow >= mListView.Items.Count) then
Exit;
if (mToRow < 0) or (mToRow >= mListView.Items.Count) then
Exit;
try
S := mListView.Items.Item[mFromRow].Caption;
mListView.Items.Item[mFromRow].Caption :=
mListView.Items.Item[mToRow].Caption;
mListView.Items.Item[mToRow].Caption := S;
except
Exit;
end;
Result := True;
end;
var
I, J: Integer;
begin
Result := False;
if not Assigned(mListView) then
Exit;
for I := 0 to mListView.Items.Count - 2 do
for J := I + 1 to mListView.Items.Count - 1 do
if mDesc then
if RightStr(mListView.Items.Item[I].Caption, mByte) <
RightStr(mListView.Items.Item[J].Caption, mByte) then
ListViewRowSwap(mListView, I, J)
else
else if RightStr(mListView.Items.Item[I].Caption, mByte) >
RightStr(mListView.Items.Item[J].Caption, mByte) then
ListViewRowSwap(mListView, I, J);
Result := True;
end;
//参数说明?:ListView,从第几字节开始排序,升序or降序
god263 2003-04-25
  • 打赏
  • 举报
回复
同意!
sysu 2003-04-20
  • 打赏
  • 举报
回复
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls;

type
TForm1 = class(TForm)
ListView1: TListView;
procedure ListView1ColumnClick(Sender: TObject; Column: TListColumn);
procedure ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
Data: Integer; var Compare: Integer);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
ColumnToSort: Integer;
IsAscSort: Boolean;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
ColumnToSort := -1;
IsAscSort := True;
end;

procedure TForm1.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
begin
if ColumnToSort <> Column.Index then
IsAscSort := True
else
IsAscSort := not IsAscSort;
ColumnToSort := Column.Index;
(Sender as TCustomListView).AlphaSort;
end;

procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
Data: Integer; var Compare: Integer);
var
ix: Integer;
begin
if ColumnToSort = 0 then
if IsAscSort then
Compare := CompareText(Item1.Caption, Item2.Caption)
else
Compare := CompareText(Item2.Caption, Item1.Caption)
else begin
ix := ColumnToSort - 1;
if IsAscSort then
Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix])
else
Compare := CompareText(Item2.SubItems[ix],Item1.SubItems[ix]);
end;
end;

end.

5,388

社区成员

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

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