Search code examples
delphidelphi-xe5items

The listviewer not change select in onshow


how can I change a selected item in listview...

here is my code sample:

 for c := 0  to Form1.LV1.Items.count -1  do
    begin
      if  (form1.lv1.Items[c].SubItems.Objects[3] as TTabSheet).TabIndex = 
        pgc1.ActivePageIndex then
      begin
        form1.lv1.Items[c].Selected:= True;
      end;
    end;

Solution

  • I created a simple application with a list view, set it into report mode, added some items, and added this OnShow event:

    procedure TForm1.FormShow(Sender: TObject);
    begin
      ListView1.Items[1].Selected := True;
    end;
    

    The specified item was indeed selected.

    The conclusion that I draw from this is that the Selected property can be used from the OnShow event. Therefore, if your program does not result in the list view selection be set, it would seem that either:

    1. The code in the question is not running at all, or
    2. The code is running, but the if statement condition is never True.

    Your next step is to debug your program. Inspect your program as it executes using your preferred debugging technique. The interactive debugger would be a sound choice.