使用 win 表单组合框 Items.AddRange 方法
我有一个对象数组,正在尝试使用 AddRange 方法将其添加到组合框控件的 Items 集合中。该方法采用 object[]
但当我向它传递已使用某些值初始化的数组的名称时,它会抱怨:
与
System.Windows.Forms.ComboBox.ObjectCollection.AddRange(object[]) 匹配的最佳重载方法有一些无效参数。
定义数组中对象的类非常简单:
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 }
};
这是我尝试调用 AddRange 的地方:
combobox1.Items.AddRange(actions);
这就是它抱怨的那一行 - 是否有一些我缺少的步骤这?当我只是添加一个简单的 string[]
时它工作得很好
I have an array of objects that I'm trying to add to the Items collection of a combo box control using the AddRange method. The method takes an object[]
but when I pass it the name of the array which has been intialized with some values, it complains:
The best overloaded method match for
System.Windows.Forms.ComboBox.ObjectCollection.AddRange(object[])
has some 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 }
};
this is where I try to call AddRange
:
combobox1.Items.AddRange(actions);
and that's the line that it's complaining about - is there some step I'm missing to be able to do this? it works fine when I'm just adding a simple string[]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 .NET C# 测试项目中进行了尝试,如下所示:效果很好。
示例代码如下:
因此您需要告诉我们您在哪里声明了 actions 对象。
I tried it out in a .NET C# test project as below & it works fine.
The sample code is as below:
So you need to tell us where have you declared the actions object.