我在ListView1的OnClick事件中,使用ListView1.Selected.Index作为判断点击的
是哪个item 后,处理相应的事。
当我点击ListView1的空白区域时就出现异常提示(project prj*.exe raised exception class EAccessViolation with message 'Access violation at address 0045A970 in module 'prj*.exe'. Read of address 00000004'. Process stopped. Use Step or Run to continue.)。
请问怎样处理这种情况。

解决方案 »

  1.   

    你在ONCLICK事件中要判断ListView1.Selected这项是否存在,如果你在空白区域点的话,应该是不存在选 中项的,所以会报错!建议你在ONCHANGE事件中写这个代码
      

  2.   

    if ListView1.Selected = nil then Exit;
      

  3.   

    To Drate(鸟窝里的虫): 你好!
    我的目的是要点击其中的的某一项后,进入另一个窗体,当前的listview 所在的窗体被释放掉。我在OnClick中用来判断其存在和执行的语句是
    if ListView.Selected.Index in [0..7] then
    case ListView.Selected.Index of
    0,4:;
    1,2,3:TreeView1.Items.Item[2].Selected := true;
    5,6,7:TreeView1.Items.Item[3].Selected := true;
    end;
    (我是用TreeView1控制不同窗体的显示,想模拟Outlook里面单击某个箱后进入这个箱子的窗口。)
    我把上面的语句放在ONCHANGE事件也是出现上面的错误。
      

  4.   

    我习惯于在SelectItem事件中写,这和你的OnClick其实一样,
    但更节约系统开销,你那样一点击就要执行,我这儿只要是选择了
    某一项才执行,岂不是好些吗?以下是我的一段代码。
    用下面两句可以判断是否点到空白。
      if lv1.SelCount<1 then 
        exit;                 procedure Tlbsd.lv1SelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
    var
      i:integer;
    begin
      if lv1.SelCount<1 then     //请注意这两行
        exit;                    //请注意这两行
      nowpx:=lv1.Selected.Index+1;
      for i:=0 to lv1.Items.Count-1 do
        lv1.Items[i].ImageIndex:=-1;
      lv1.Items.Item[nowpx-1].ImageIndex:=16;
    end;
      

  5.   

    解决了。
    To Drate(鸟窝里的虫) ,genphone_ru(票票) 谢谢!
      

  6.   

    if ListView.selected<>nil then ////////////////////////
    if ListView.Selected.Index in [0..7] then
    case ListView.Selected.Index of
    0,4:;
    1,2,3:TreeView1.Items.Item[2].Selected := true;
    5,6,7:TreeView1.Items.Item[3].Selected := true;
    end;