将多个类项添加到多个组合框中的替代方法?C#

将多个类项添加到多个组合框中的替代方法?C#,c#,winforms,C#,Winforms,我有一个鱼类类,我试图在一个组合框中显示不同的物种,我现在做的方式太单调了,必须有更好的方式 在“Species1”中,你可以看到我试图在所有4个组合框中添加相同的species 我有4个组合框,我想在所有4个组合框中显示这9种鱼类,让用户选择他捕获的4种鱼类 Species1 = new Fish("Angler",5); Catch1ComboBox.Items.Add(Species1.Getspecies()); C

我有一个鱼类类,我试图在一个组合框中显示不同的物种,我现在做的方式太单调了,必须有更好的方式

在“Species1”中,你可以看到我试图在所有4个组合框中添加相同的species

我有4个组合框,我想在所有4个组合框中显示这9种鱼类,让用户选择他捕获的4种鱼类

            Species1 = new Fish("Angler",5);
            Catch1ComboBox.Items.Add(Species1.Getspecies());
            Catch2ComboBox.Items.Add(Species1.Getspecies());
            Catch3ComboBox.Items.Add(Species1.Getspecies());
            Catch4ComboBox.Items.Add(Species1.Getspecies());
            Catch1ComboBox.SelectedIndex = 0;

            Species2 = new Fish("Cod", 3);
            Catch1ComboBox.Items.Add(Species2.Getspecies());

            Species3 = new Fish("Haddock", 4);
            Catch1ComboBox.Items.Add(Species3.Getspecies());

            Species4 = new Fish("Hake", 1);
            Catch1ComboBox.Items.Add(Species4.Getspecies());

            Species5 = new Fish("Horse Mackerel", 0.5m);
            Catch1ComboBox.Items.Add(Species5.Getspecies());

            Species6 = new Fish("Witches", 3);
            Catch1ComboBox.Items.Add(Species6.Getspecies());

            Species7 = new Fish("Plaice", 8);
            Catch1ComboBox.Items.Add(Species7.Getspecies());

            Species8 = new Fish("Skate and Rays", 1.8m);
            Catch1ComboBox.Items.Add(Species8.Getspecies());

            Species9 = new Fish("Whiting", 7);
            Catch1ComboBox.Items.Add(Species9.Getspecies());

首先,将您的所有物种列入列表:

var物种=新列表{
新鱼(“钓鱼者”,5)。GetSpecies(),
新鱼(“鳕鱼”,3)。GetSpecies(),
新鱼(“黑线鳕”,4)。GetSpecies(),
新鱼(“鳕鱼”,1)。GetSpecies(),
新鱼(“马鲭鱼”,0.5m)。GetSpecies(),
新鱼(“女巫”,3)。GetSpecies(),
新鱼(“鲽鱼”,8)。GetSpecies(),
新鱼(“溜冰鱼和鳐鱼”,1.8米),
新的鱼类(“鳕鱼”,7)
}
然后将列表的副本设置为所有
组合框
对象的
数据源

Catch1ComboBox.DataSource=新绑定列表(物种);
Catch2ComboBox.DataSource=新绑定列表(物种);
Catch3ComboBox.DataSource=新绑定列表(物种);
Catch4ComboBox.DataSource=新绑定列表(物种);
使用
newbindinglist
是为了防止所有组合框挂接到同一个源,从而强制保持相同的值


有一个例子。

上下文关键字“var”只能出现在局部变量声明或脚本代码中“有什么想法吗?”对不起,我对classesput方法中的代码非常陌生,例如表单的构造函数。我把它放在方法中,似乎已经修复了它,但它产生了一个有趣的小错误,当在单个组合框中选择一条鱼时,所有4条都是同步的,并随它而改变。“使用泛型类型“BindingList”需要1个类型参数”我添加了对象类型。