我在使用Delphi7.0的ListView组件排序的时候使用Customsort排序方法,当我在前面定义了一个function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall; 函数,
可我在后面用CustomSort(@CustomSortProc,0),调用的时候却老是提示 Variable required,为什么?为什么?急死了!

解决方案 »

  1.   

    //Help里是这样写的,你先看看是否正常
    //如果正常,估计问题出现在你自己定义的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;
      

  2.   

    忘记了,这个是我写的被调用的函数:
    function customSort(Item1, Item2: TListItem; ParamSort: Integer): Integer; stdcall;
    begin
     Result:=lstrcmp(PChar(TListItem(Item1).Caption),   
                                                PChar(TListItem(Item2).Caption));end;
      

  3.   

    我测试你的代码没发现问题
    Help里的代码在你的环境下如何?
      

  4.   

    不可以的,我试了的。但是我其实下午我开始做的时候是可以运行的。CustomSort(@CustomSortProc,0),当我用CustomSort(@Tform1.CustomSortProc,0),代替时是可以运行的,但是在点击了Column 触发事件了以后,却提示Access内存错误。
      

  5.   

    Tform1.CustomSortProc
    不能用类里的方法,procedure of object; procedure; 是不同的类型
    类方法有Self域(实际上是就是普通方法的第一个参数)
    可以参考如下代码访问Form1
    CustomSort(@customSort, Integer(Self))function customSort(Item1, Item2: TListItem; ParamSort: Integer): Integer; stdcall;
    begin;
      TForm1(ParamSort);
    end;
      

  6.   

    大概是什么意思能不能解释一下?能不能把你的QQ留给我,出学delphi好多问题想请教
      

  7.   

    把Help中的例子作了一下,没有任何问题。然后把你的也作了一下,还是OK。
    我自己也写了一个,你看看能不能在你的环境中运行?
    (代码是仿照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.