C#;列表框作为一个对象(容器) [英] C#; listbox's as one object(container)
问题描述
我使用Visual Studio 2008
我的表单上有5个列表框,我创建了一个新的类文件-称为他"scaner.cs"
I use Visual studio 2008
I have 5 listbox's on form,I created a new class file -called him "scaner.cs"
scaner.cs-他看不到列表框".
scaner.cs -he cannot see "listbox".
我已经创建了一个实例.
scaner Comp = new scaner(listBox2, listBox1, listBox3, listBox4, listBox5);
I have create an instance.
scaner Comp = new scaner(listBox2, listBox1, listBox3, listBox4, listBox5);
在scaner.cs文件中,我像这样使用它.
In scaner.cs file I use it like this.
class scaner
{
public ListBox ls;
public ListBox lsE;
public ListBox lsIVars;
public ListBox lsNumbers;
public ListBox lsStrings;
public scaner(ListBox ls, ListBox lsE, ListBox lsIVars, ListBox lsNumbers, ListBox lsStrings)
{
this.ls = ls;
this.lsE = lsE;
this.lsIVars = lsIVars;
this.lsNumbers = lsNumbers;
this.lsStrings = lsStrings;
}
}
我的问题:如何将这个大代码替换为更舒适"的方法.
My question : How can i replaced this big code to more "comfortably" method.
scaner Comp = new scaner(listBox2, listBox1, listBox3, listBox4, listBox5);
如果我有5个以上的列表框,那将是可怕的. 我如何才能加入另一个类文件列表框" 感谢您的回答.
IF i had more then 5 listbox's ,it will be awful. How can i acced form another class file "Listbox's" Thanks for answers.
推荐答案
创建一个字段来存储所有ListBox
实例,然后更改构造函数以接受任意数量的实例:
Create a field to store all the ListBox
instances and then change the constructor to accept an arbitrary number of them:
class scaner
{
readonly IEnumerable<ListBox> listBoxes;
public IEnumerable<ListBox> ListBoxes
{
get { return this.listBoxes; }
}
public scaner(params ListBox[] listBoxes)
{
this.listBoxes = listBoxes;
}
}
这将允许您执行以下操作:
This will allow you to do this:
scaner Comp = new scaner(listBox1, listBox2);
或者这个:
// Here I am passing 4 ListBoxes - you can pass as many as you wish
// without modifying the source code of your scaner class
scaner Comp = new scaner(listBox1, listBox2, listBox3, listBox4);
这篇关于C#;列表框作为一个对象(容器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!