Mega Code Archive

 
Categories / Delphi / VCL
 

Selecting Delphis TListView Item on Item Check UnCheck

Title: Selecting Delphi's TListView Item on Item Check / UnCheck The TListView Delphi control displays a list of items in a fashion similar to how Windows Explorer displays files and folders. Items can be displayed in columns with column headers and sub-items, or vertically or horizontally, with small or large icons. When the Checkboxes property is True, ListView includes a check box next to the items in the list. To get the "checked" state for an item in the list view, read the Checked boolean property. Check/Uncheck Does Not Select the Item You will notice that checking or unchecking the item in a list view will not change the selected item - the item that gets checked will not get selected. If you need to have the item Selected when Checked, you need to handle the OnMouseDown even of the TListView: //ListView OnMouseDown procedure TForm.ListView1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var li: TListItem; lv : TListView; begin lv := TListView(Sender); li := lv.GetItemAt(X, Y); if (li nil) AND (lv.Selected li) then lv.Selected := li; end; With the above code for the OnMouseDown event handler, when an item is checked or unchecked it will get selected. Note: you can also notice that there's no event that you can handle when an item is checked or unchecked. The Implementing the On Item Checked Event for the TListView Control provides the code you can use to handle the item checked state change event.