gpt4 book ai didi

delphi - 如何使用此 CustomSort 函数对 ListView 进行排序?

转载 作者:行者123 更新时间:2023-12-03 18:17:10 24 4
gpt4 key购买 nike

如果 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;

这会提示错误。//访问冲突

但是如果我们将 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;

如何修复它。请帮忙。非常感谢。

最佳答案

您不能在回调函数中访问 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]);

当然,如果这是您唯一需要的,您可以在“OptionalParam”中发送 col 的值。或者,您可以将“col”设为全局变量而不是字段,或者使用“Form1”全局变量本身,如果未注释掉,IDE 会将其放在实现部分之前。

您还可以使用 OnCompare事件。

关于delphi - 如何使用此 CustomSort 函数对 ListView 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4096081/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com