简体   繁体   English

c#组合框显示arraylist项

[英]c# combobox displaying arraylist items

I want my combo box to list all my array list items.我希望我的组合框列出我所有的数组列表项。 This is what i have so far but i don't know what i can add to make it display each array list item into the combo box.这是我到目前为止所拥有的,但我不知道我可以添加什么以使其将每个数组列表项显示到组合框中。 Is there a way i can write Items.Display or something along those lines?有没有办法我可以写 Items.Display 或类似的东西?

public void eh()
{
snip
}
public void PopulateActors()
{
    cboActor.Items.Clear(); 
    cboActor.Items.AddRange(ActorArrayList.Cast<string>());
}

You can use DataSource to bind the ArrayList to your combobox:您可以使用DataSourceArrayList绑定到您的组合框:

yourComboBox.DataSource = yourArrayList;

Use DisplayMember and ValueMember to select what is displayed and what is evaluated as Value of the item:使用DisplayMemberValueMember选择显示的内容以及评估为项目Value的内容:

yourComboBox.DisplayMember = "Displayed thing";
youtComboBox.ValueMember = "Evaluated thing";

If you don't specify the DisplayMember , the ToString() will be called on each item to get the displayed string instead.如果您未指定DisplayMember ,则会在每个项目上调用ToString()以获取显示的字符串。 In your case, it looks like you have an ArrayList of string, so you don't need to specify any values for DisplayMember and ValueMember .在您的情况下,您似乎有一个字符串ArrayList ,因此您无需为DisplayMemberValueMember指定任何值。

NOTE : You should use a List<T> instead, it would be better.注意:您应该改用List<T> ,这样会更好。 ArrayList is just an old stuff. ArrayList只是一个旧东西。

You can create an array list like this您可以像这样创建一个数组列表

ArrayList sampleArray = new ArrayList();
            sampleArray.Add("India");
            sampleArray.Add("China");
            sampleArray.Add("USA");
            sampleArray.Add("UK");
            sampleArray.Add("Japan");

and then can add it to your combobox然后可以将其添加到您的组合框

cboActor.Items.Clear(); 
 cboActor.Items.AddRange(sampleArray.ToArray());

You need to create ComboBoxItems in a loop and add them one by one:您需要循环创建 ComboBoxItems 并一一添加:

ComboboxItem item = new ComboboxItem();
item.Text = "Item text1";
item.Value = 12;

cboActor.Items.Add(item);

Hope this helps :)希望这有帮助:)

foreach (string line in ActorArrayList)
{
    cboActor.Items.Add(line);        
}

You have to add a listItem(Text,Value)你必须添加一个 listItem(Text,Value)

foreach (Actor line in ActorArrayList)
    {
        cboActor.Items.Add(new ListItem( line.Name ,line.ID)); //as second part you may enter the ID of the object so you can use it at a later time
    }
}

Another example not mentioned above!上面没有提到的另一个例子! Similar to Sid M, since that didn't work for me, I share the solution I found for me:与 Sid M 类似,由于这对我不起作用,我分享了我为我找到的解决方案:

String[] data = new String[]{"Data1", "Data2", "Data3", "Data4"};

cboData.Items.AddRange(data);

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

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