首页  编辑  

监测ListView的CheckBox点击

Tags: /超级猛料/VCL/ListView/   Date Created:

下面的代码都有BUG,就是如果用Space来Check Box,那么,不会有效果!

请在ListView的OnMouseUp中设置:

procedure TForm1.ListView1MouseUp(Sender: TObject; Button: TMouseButton;

 Shift: TShiftState; X, Y: Integer);

var

 Item: TListItem;

 HitTest: THitTests;

begin

 // Which item belongs to the checkbox

 Item := ListView1.GetItemAt(x, y);

 // What kind of thing was hit on the item

 HitTest := ListView1.GetHitTestInfoAt(x, y);

 // If an Item was hit and exactly his checkbox

 if (Item <> nil) and (HitTest = [htOnStateIcon]) then

 begin

   ////////////////////////////////

   // Handle OnCheck here        //

   ////////////////////////////////

   // Example

   //

   //  if Item.Checked = False then

   //  begin

   //    if (Item.Index = 0) or (ListView1.Items.Item[Item.Index - 1].Checked = True) then

   //      Item.Checked := True else Item.Checked := False;

   //  end else

   //    begin

   //    if (Item.Index = ListView1.Items.Count - 1) or (ListView1.Items.Item[Item.Index + 1].Checked = False) then Item.Checked := False else

   //      Item.Checked := True;

   //  end;

 end;

end;

*********************************

也可以用下面的代码,放在TreeView的OnChange事件中:

var

 Item              : TListItem;

 Pos               : TPoint;

begin

 if Item.Checked then

 ............

end;