简体   繁体   English

Delphi 7-如何将ListView项分配到组合框

[英]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 但我收到此错误:无法将TListItems分配给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). 您不能将ListView.Items添加到ComboBox.Items (正如编译器告诉您的,一个是TListItems集合,另一个是TStrings的后代,并且它们与类型不兼容)。 You can add the caption of a selected ListItem to the ComboBox.Items . 您可以将所选ListItem的标题添加到ComboBox.Items

You need to add it to the ComboBox.Items: 您需要将其添加到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 ): 如果您只想将ListView中的所有项目添加到ComboBox (这似乎毫无意义,因为它们已经显示在ListView ):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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