1

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?

public void eh()
{
snip
}
4

7 回答 7

3
public void PopulateActors()
{
    cboActor.Items.Clear(); 
    cboActor.Items.AddRange(ActorArrayList.Cast<string>());
}
于 2013-09-25T09:09:24.403 回答
2

You can use DataSource to bind the ArrayList to your combobox:

yourComboBox.DataSource = yourArrayList;

Use DisplayMember and ValueMember to select what is displayed and what is evaluated as Value of the item:

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. 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.

NOTE: You should use a List<T> instead, it would be better. ArrayList is just an old stuff.

于 2013-09-25T09:10:49.217 回答
1

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());
于 2013-09-25T09:14:04.747 回答
0

You need to create ComboBoxItems in a loop and add them one by one:

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

cboActor.Items.Add(item);

Hope this helps :)

于 2013-09-25T09:08:21.733 回答
0
foreach (string line in ActorArrayList)
{
    cboActor.Items.Add(line);        
}
于 2013-09-25T09:08:36.250 回答
0

You have to add a 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
    }
}
于 2013-09-25T09:12:46.443 回答
0

Another example not mentioned above! Similar to Sid M, since that didn't work for me, I share the solution I found for me:

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

cboData.Items.AddRange(data);
于 2017-12-18T01:00:02.127 回答