Using the combo box of the winning form Items.AddRange method
I have an array of objects that I am trying to add to the Items collection of a combo box using the AddRange method. The method accepts object[]
, but when I pass it the name of an array that has been indexed with some values, it complains:
The best overloaded method match for
System.Windows.Forms.ComboBox.ObjectCollection.AddRange(object[])
has multiple invalid arguments.
The class defining the objects in my array is very simple:
public class Action
{
public string name;
public int value;
public override string ToString()
{
return name;
}
}
and my array is declared such:
public Action[] actions = new Action[] {
new Action() { name = "foo", value = 1 },
new Action() { name = "bar", value = 2 },
new Action() { name = "foobar", value = 3 }
};
here i am trying to call AddRange
:
combobox1.Items.AddRange(actions);
and that's the line it is complaining about - is there some step I am missing to get it done? it works great when i just add a simplestring[]
source to share
I tried this in a .NET C # test project as shown below and it works great. Sample code looks like this:
public partial class Form1 : Form
{
public Action[] actions = new Action[]
{
new Action() { name = "foo", value = 1 },
new Action() { name = "bar", value = 2 },
new Action() { name = "foobar", value = 3 }
};
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.AddRange(actions);
}
}
public class Action
{
public string name;
public int value;
public override string ToString()
{
return name;
}
}
So, you have to tell us where you declared the action object.
source to share