我想获得一个Frame里的Listview1的项目数,代码是Listview1.Items.Count,结果总是0(Listview1有6条数据)。此时如果访问Listview1.Items[5]则正常没有错误,之后再Listview1.Items.Count则返回6.请问是什么原因?是不是和Frame有关?谢谢大家~

解决方案 »

  1.   

    你是在什么事件中去取得Listview1.Items.Count的?如果是在创建的时候取得,而Listview1.Items并没有值,那当然就为0了,当Listview1.Items创建完毕后,再Listview1.Items.Count当然有值了。
      

  2.   

    @zhao_yong:
    Listview1的数据是设计时就赋予的,
    另外我的代码是这样的:
    //此时已经创建完了
    i:=Listview1.Items.Count;//i=0
    s:=Listview1.Items[5].Caption;//s有值
    [/code]
      

  3.   

    你用的是哪一版,难道发现其中的Bug了?

    结果总是0(Listview1有6条数据)。此时如果访问Listview1.Items[5]则正常没有错误,之后再Listview1.Items.Count则返回6
    ”是不是i是全局变量,刚赋值就被别处修改了?
      

  4.   

    i:=Listview1.Items.Count;
    此句之后必须用到变量i,才能得到i的值,否则,delphi,会认为i为无用变量,被忽略i:=Listview1.Items.Count;
    ShowMessage(IntToStr(i));//i:=6;
      

  5.   

    i:=Listview1.Items.Count;
    此句之后必须用到变量i,才能得到i的值,否则,delphi,会认为i为无用变量,被忽略i:=Listview1.Items.Count;
    ShowMessage(IntToStr(i));//i:=6;
      

  6.   

    i:=Listview1.Items.Count;
    此句之后必须用到变量i,才能得到i的值,否则,delphi,会认为i为无用变量,被忽略i:=Listview1.Items.Count;
    ShowMessage(IntToStr(i));//i:=6;
      

  7.   

    Delphi2007和Delphi2009都是这样的
    我新建了一个工程,主窗体是Unit1,Frame的单元是Unit2.Frame里只放了一个Listview,里面有数据,没写其它代码。
    Unit1的代码如下:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls,
      {引用Unit2(即Frame2所在单元)}
      Unit2;type
      TForm1 = class(TForm)
        btn1: TButton;
        procedure btn1Click(Sender: TObject);
      private
        //在这儿声明
        myframe:TFrame2;
      end;var
      Form1: TForm1;implementation
    {$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);
    begin
      myframe:=TFrame2.Create(Self);
      (*设置myframe为不可见。问题出在这里,
      如果去掉这句则ShowMessage(IntToStr(myframe.lv1.Items.Count))显示6
      如果保留则显示0;
      调试发现只有当myframe显示到窗口上(被panel等盖住不算)才显示6
      *)
      myframe.Visible:=False;
      myframe.Parent:=Self;
      ShowMessage(IntToStr(myframe.lv1.Items.Count));
    end;end.
    请问怎么能保持myframe.Visible:=False;而正常显示出Items.Count呢?谢谢大家~
      

  8.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls,Unit2,CommCtrl;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
         myframe:TFrame2;
      public  end;var
      Form1: TForm1;implementation
         {$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);begin
       myframe := TFrame2.Create(self);
       myframe.Visible := false;
       myframe.Parent:=Self;
      showMessage(inttostr(SendMessage(myframe.ListView1.Handle, LVM_GETITEMCOUNT, 0, 0)));
      ShowMessage(IntToStr(myframe.ListView1.Items.Count));
    end;end.
      怎么说呢,原则上来说这是BORLAND的一个笨蛋写的。呵呵
      

  9.   

    @starluck
    ~谢谢每次都回答我的问题:-)
    只是不太明白为什么myframe.lv1.Items.Count在frame显示前就获取不了呢?
      

  10.   


    這跟PARENT在關系,也就是LISTVIEW所在的OWNER有關系,你可翠看下LISTVIEW取COUNT的代碼就會明白了 
      

  11.   

    ddddddddddddddddddddddddddddddddddddddddddd