使用win form组合框Items.AddRange方法

kin*_*uta 2 .net c# combobox addrange winforms

我有一个对象数组,我正在尝试使用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 }
};
Run Code Online (Sandbox Code Playgroud)

这是我试图打电话的地方AddRange:

combobox1.Items.AddRange(actions);
Run Code Online (Sandbox Code Playgroud)

这就是它抱怨的那条线 - 是否有一些步骤我不知道能够做到这一点?当我只是添加一个简单的时候它工作正常string[]

Gan*_* R. 6

我在.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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

所以你需要告诉我们你在哪里声明了actions对象.