我在使用Delphi7.0的ListView组件排序的时候使用Customsort排序方法,当我在前面定义了一个function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall; 函数,
可我在后面用CustomSort(@CustomSortProc,0),调用的时候却老是提示 Variable required,为什么?为什么?急死了!
可我在后面用CustomSort(@CustomSortProc,0),调用的时候却老是提示 Variable required,为什么?为什么?急死了!
//如果正常,估计问题出现在你自己定义的CustomSortProc函数中
//贴出能调试的代码来分析
function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
begin
Result := -CompareText(Item1.Caption,Item2.Caption);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ListView1.CustomSort(@CustomSortProc, 0);
end;
function customSort(Item1, Item2: TListItem; ParamSort: Integer): Integer; stdcall;
begin
Result:=lstrcmp(PChar(TListItem(Item1).Caption),
PChar(TListItem(Item2).Caption));end;
Help里的代码在你的环境下如何?
不能用类里的方法,procedure of object; procedure; 是不同的类型
类方法有Self域(实际上是就是普通方法的第一个参数)
可以参考如下代码访问Form1
CustomSort(@customSort, Integer(Self))function customSort(Item1, Item2: TListItem; ParamSort: Integer): Integer; stdcall;
begin;
TForm1(ParamSort);
end;
我自己也写了一个,你看看能不能在你的环境中运行?
(代码是仿照HELP中TreeView的CustomSort Example写的)
type
TForm1 = class(TForm)
ListView1: TListView;
procedure FormCreate(Sender: TObject);
procedure ListView1ColumnClick(Sender: TObject; Column: TListColumn);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1;
FLastSortedColumn: integer;
FAscending: boolean;implementation{$R *.dfm}function CustomSortProc(Item1, Item2: TListItem; Data: integer): integer; stdcall;
begin
if Data = 0 then
Result := AnsiCompareText(Item1.Caption, Item2.Caption)
else
Result := AnsiCompareText(Item1.SubItems[Data-1],
Item2.SubItems[Data-1]);
if not FAscending then Result := -Result;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
FLastSortedColumn := -1;
FAscending := True;
end;procedure TForm1.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
begin
if Column.Index = FLastSortedColumn then
FAscending := not FAscending
else
FLastSortedColumn := Column.Index;
TListView(Sender).CustomSort(@CustomSortProc, Column.Index);
end;end.