Home > database >  How to display / capture diffrents values of selected TListViewItem from "OnItemClick" eve
How to display / capture diffrents values of selected TListViewItem from "OnItemClick" eve

Time:07-17

i have an application with 2 TLabels, 1 TListView. I would like display the value or content(Text) of TListViewItem inside the TLabels in in way that the content of the first TLabel can't be the same. My code:

....
  ListView1: TListView;
  Base: TLabel;
  Hypo: TLabel;
....

procedure TMainForm.BaseClick(Sender: TObject);
begin
   ListView1.Visible := True;
end;

procedure TMainForm.HypoClick(Sender: TObject);
begin
   ListView1.Visible := True;
end;    

procedure TMainForm.ListView1ItemClick(const Sender: TObject;
   const AItem: TListViewItem);
begin
   if Assigned(ListView1.Selected) then
      Base.Text := TListViewItem(ListView1.Selected).Text;
   
      Hypo.Text := TListViewItem(ListView1.Selected).Text;
   
   ListView1.Visible := False;
end;

I used LiveBindings to fill the TListView; when i run the app and select one item it works but it's displaying the same value/content in both TLabels TListViewwith2labels

CodePudding user response:

My first reaction, if your listview is livebinded then why don't you use livebindings to link your 2 labels ?

Second one is your code, you use Selected when you have the AItem parameter so

procedure TMainForm.ListView1ItemClick(const Sender: TObject;
   const AItem: TListViewItem);
begin
  Base.Text:= AItem.text;
  Hypo.Text:= AItem.detail;
  ListView1.Visible := False;
end;  

should be sufficient if it is not a DynamicAppearance type.

CodePudding user response:

If you really have 2 selected items, then you have to iterate through the whole list view

procedure TForm3.ListView1ItemClick(const Sender: TObject;
  const AItem: TListViewItem);
var elvitem : TListViewItem;
    i,n : integer;
begin
n:=0;
for i:=0 to ListView1.ItemCount do
 begin
   if ListView1.Items[i].Purpose=TListItemPurpose.None then // it's an item 
    begin
      if ListView1.Items[i].Checked then
      begin
       inc(n);
      case n of
        1 : base.text:=ListView1.Items[i].Text;
        2 : begin
              hypo.text:=ListView1.Items[i].Text;
              break; // don't search more
            end;
      end;
      end;
    end;
 end;

Here item 2 and 8 are selected with this code

procedure TForm3.FormCreate(Sender: TObject);
begin
Listview1.Items[2].Checked:=True;
Listview1.Items[8].Checked:=True;
end;

here item 2 and 8 are selected

  • Related