如果customsort功能与变量传递的,现在看来,这将访问冲突.
public
...
col: integer;
...
Procedure listviewcol;
begin
col:=5
...
end;
procedure TForm1.sortcol(listview: tlistview);
function CustomSortProc(Item1,Item2: TListItem;
OptionalParam: integer): integer;stdcall;
begin
Result := AnsiCompareText(Item2.subitems.Strings[col], Item1.subitems.Strings[col]);
end;
begin
ListView.CustomSort(@CustomSortProc,0);
end;Run Code Online (Sandbox Code Playgroud)
这会提示错误.//访问冲突
但是如果我们将AnsicompareText中的col更改为5,那么它运行良好.
procedure TForm1.sortcol(listview: tlistview);
function CustomSortProc(Item1,Item2: TListItem;
OptionalParam: integer): integer;stdcall;
begin
Result := AnsiCompareText(Item2.subitems.Strings[5], Item1.subitems.Strings[5]);// it works.
end;
begin
ListView.CustomSort(@CustomSortProc,0);
end;
Run Code Online (Sandbox Code Playgroud)
如何解决它.请帮忙.非常感谢.
您无法访问col回调函数内部,它不是表单的方法.在方法中嵌套回调的技巧是徒劳的.;)如果您需要访问表单字段,那么使用它OptionalParam可以在回调中引用您的表单.
begin
ListView.CustomSort(@CustomSortProc, Integer(Self));
[...]
function CustomSortProc(Item1,Item2: TListItem;
OptionalParam: integer): integer; stdcall;
var
Form: TForm1;
begin
Form := TForm1(OptionalParam);
Result := AnsiCompareText(Item2.subitems.Strings[Form.col],
Item1.subitems.Strings[Form.col]);
Run Code Online (Sandbox Code Playgroud)
当然,col如果这是您唯一需要的,您可以发送'OptionalParam' 的值.或者,您可以将'col'设置为全局变量而不是字段,或者使用"Form1"全局变量本身,如果未注释掉,IDE将在实现部分之前放置该变量.
您还可以使用OnCompare事件.