使用 win 表单组合框 Items.AddRange 方法

发布于 2024-08-03 01:38:08 字数 833 浏览 9 评论 0原文

我有一个对象数组,正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一生独一 2024-08-10 01:38:08

我在 .NET C# 测试项目中进行了尝试,如下所示:效果很好。
示例代码如下:

 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;
        }
    }

因此您需要告诉我们您在哪里声明了 actions 对象。

I tried it out in a .NET C# test project as below & it works fine.
The sample code is as below:

 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 need to tell us where have you declared the actions object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文