简体   繁体   中英

Delphi 7 - How to assign ListView items into a combobox

I'm using this code to assign values:

combobox1.Text:=form1.listview1.Selected.Caption;

But i'm getting this error: Cannot assign a TListItems to a TComboBox

You can't add a ListView.Items to a ComboBox.Items (as the compiler has told you, one is a TListItems collection and the other is a descendant of TStrings , and they're not type compatible). You can add the caption of a selected ListItem to the ComboBox.Items .

You need to add it to the ComboBox.Items:

ComboBox1.Items.Add(ListView1.Selected.Caption);

If you want to add all selected items, you need to use a loop:

var
  Item: TListItem;
begin
  Item := ListView1.Selected;
  while Item <> nil do
  begin
    ComboBox1.Items.Add(Item.Caption);
    Item := ListView1.GetNextItem(Item, sdAll, [isSelected]);
  end;

If you just want to add all items from the ListView to the ComboBox (which seems pretty pointless, as they're already displayed in the ListView ):

var
  i: Integer;
begin
  for i := 0 to ListView1.Items.Count - 1 do
    ComboBox1.Items.Add(ListView1.Items[i].Caption);
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM