怎么才能实现像资源管理器那样的排序功能,能够倒序和顺序!!
解决方案 »
- DBgridEh特定行的显示风格问题,在线给分。
- 如何实现重复运行同一个程序时把这个程序的所有实例都关闭!
- 无法找到程序输入点GetTextExtentPointI(在动态连接库MSDART.DLL)
- 怎么在程序中提取到access数据库(mdb)中的字段注释?
- 有关“Spcomm”控件的问题——————在线等待!!!
- C/S开发的问题
- Delphi下如何判断一个目录是不是存在?
- 各位高手,一个关于DBGRID的问题?
- 想问一个小问题,可是总解决不了。
- 哪位有在DELPHI下显示一幅位图的经验 ?
- 在线等待---BORLAND中的GDB数据库文件用户名为SYSDBA,怎么一直打不开?
- 关于数据库表的问题
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.
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降序
十分感谢你的代码,我刚刚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