C# 像C中的数组一样使用combobox?

C# 像C中的数组一样使用combobox?,c#,arrays,combobox,C#,Arrays,Combobox,我的项目有问题。但我找不到正确的答案。我使用了很多组合框和文本框。我有这个功能: private void combo1add(string options, int combono) { openconnection(); //Function that open mysql connection if (combono==1) comboBox1.Items.Clear(); if (combono==6) comboBox

我的项目有问题。但我找不到正确的答案。我使用了很多组合框和文本框。我有这个功能:

    private void combo1add(string options, int combono)
    { 
        openconnection();  //Function that open mysql connection
        if (combono==1) comboBox1.Items.Clear();
        if (combono==6) comboBox6.Items.Clear();
        if (combono == 7) comboBox7.Items.Clear();
        string query = "select * from material";
        if (options == "Non Ceramic")
        {
            query = "select * from material where type='metal'";
        }
        command = new MySqlCommand(command, connection);
        MySqlDataReader reader = komut.ExecuteReader();
        if (combono == 1)
        {
            while (reader.Read())
            {
                comboBox1.Items.Add(oku["materialno"].ToString());
            }
        }
        if (combono == 6)
        {
            while (oku.Read())
            {
                comboBox6.Items.Add(oku["materialno"].ToString());
            }
        }
        if (combono == 7)
        {
            while (oku.Read())
            {
                comboBox7.Items.Add(oku["materialno"].ToString());
            }
        }
    }
我的项目中有这个代码。但我有超过20个combobox,我想在示例中像数组一样使用它:我也想像数组一样使用textbox,但当我尝试时,它总是出错

while (oku.Read())
{
    comboBox[i].Items.Add(oku["materialno"].ToString());
}

但是我的研究不能成功。我在c有新的工作。对不起,我的英语不好

此代码将表单中的所有组合框放入一个数组中:

public partial class Form1 : Form
{
    ComboBox[] cbs;

    public Form1()
    {
        InitializeComponent();
        cbs = getControls<ComboBox>(this).ToArray();
        foreach (var cb in cbs) MessageBox.Show(cb.Name);
    }

    IEnumerable<T> getControls<T>(Control top) where T : Control
    {
        var result = new List<T>();
        foreach (Control c in top.Controls) {
            if (c is T)
            {
                result.Add((T)c);
            }
            result.AddRange(getControls<T>(c));
        }
        return result;
    }
}

getControls过程可用于收集任何类型的控件,例如Button

看看如何改进您的帖子以获得更多答案。将组合框添加到数组、列表或字典中,您可以通过索引或键访问每个组合框。谢谢nvoigt,我会记住它。对不起,这是我的第一个问题,我会及时了解规则的。你给了我一个想法,我想做什么,但问题是我怎么做?非常感谢!!!İt工作正常,但它得到了相反的数组,比如combobox1,combobox2,combobox3=cb[0]=combobox3,cb[1]=combobox2,cb[2]=combobox1。有没有办法纠正这个问题?这样很有用,但我必须进行转换。若要反转任何集合中元素的顺序,请使用反向扩展方法:getControlsthis.reverse.ToArray