我有一系列的对象,我试着要添加到项目收集的一种组合框控制使用AddRange方法。方法需要一个 object[] 但是当我通过它的名称列已intialized有一些值,也抱怨:

最好的超载方法的匹配 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[]

有帮助吗?

解决方案

我尝试过了。净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;
        }
    }

所以你需要告诉我们你在哪里宣布的行动的对象。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top