获取在方法C#WinForms中使用的实际使用的GUI元素(comboBox)的名称

时间:2017-08-25 15:26:43

标签: c# winforms

我有一个从comboBox中删除特殊项的方法。 首先,组合框中填充了来自数据库的表名,然后我想删除列表中不需要的一些组合。 我这样做是通过同时填充和删除所有组合框的项目。但是现在我只想要实际使用的组合框,并且在Dropdown上删除了项目。所以我认为我需要获取实际使用的comboBox的名称,以将其用作变量或类似的东西。我该怎么做?

这是我的代码,直到现在。你看到它只是添加和删除一个comboBox的项目很长。

        //Load DB Tables when Drop Down the Dropboxes. And remove not used/needed tables from Combobox.
    private void referenzDropDown(object sender, EventArgs e)
    {
        string myInsertQuery = "SHOW TABLES";
        MySqlCommand myCommand = new MySqlCommand(myInsertQuery, myConnection);
        MySqlDataReader myReader;
        myReader = myCommand.ExecuteReader();
        comboBox18.Items.Clear();
        comboBox19.Items.Clear();
        comboBox20.Items.Clear();
        comboBox21.Items.Clear();
        comboBox22.Items.Clear();
        comboBox23.Items.Clear();
        comboBox24.Items.Clear();
        comboBox25.Items.Clear();
        comboBox26.Items.Clear();
        if (myReader.HasRows == true)
        {
            while (myReader.Read())
            {
                comboBox18.Items.Add((string)myReader[0]);
                comboBox19.Items.Add((string)myReader[0]);
                comboBox20.Items.Add((string)myReader[0]);
                comboBox21.Items.Add((string)myReader[0]);
                comboBox22.Items.Add((string)myReader[0]);
                comboBox23.Items.Add((string)myReader[0]);
                comboBox24.Items.Add((string)myReader[0]);
                comboBox25.Items.Add((string)myReader[0]);
                comboBox26.Items.Add((string)myReader[0]);
            }
            myReader.Close();
        }
        comboBox18.Items.Remove("referenzen");
        comboBox18.Items.Remove("waage1");
        comboBox18.Items.Remove("waage2");
        comboBox18.Items.Remove("waage3");
        comboBox18.Items.Remove("waage4");
        comboBox18.Items.Remove("waage5");
        comboBox18.Items.Remove("waage6");
        comboBox18.Items.Remove("waage7");
        comboBox18.Items.Remove("waage8");

        comboBox19.Items.Remove("referenzen");
        comboBox19.Items.Remove("waage1");
        comboBox19.Items.Remove("waage2");
        comboBox19.Items.Remove("waage3");
        comboBox19.Items.Remove("waage4");
        comboBox19.Items.Remove("waage5");
        comboBox19.Items.Remove("waage6");
        comboBox19.Items.Remove("waage7");
        comboBox19.Items.Remove("waage8");

        comboBox20.Items.Remove("referenzen");
        comboBox20.Items.Remove("waage1");
        comboBox20.Items.Remove("waage2");
        comboBox20.Items.Remove("waage3");
        comboBox20.Items.Remove("waage4");
        comboBox20.Items.Remove("waage5");
        comboBox20.Items.Remove("waage6");
        comboBox20.Items.Remove("waage7");
        comboBox20.Items.Remove("waage8");

        comboBox21.Items.Remove("referenzen");
        comboBox21.Items.Remove("waage1");
        comboBox21.Items.Remove("waage2");
        comboBox21.Items.Remove("waage3");
        comboBox21.Items.Remove("waage4");
        comboBox21.Items.Remove("waage5");
        comboBox21.Items.Remove("waage6");
        comboBox21.Items.Remove("waage7");
        comboBox21.Items.Remove("waage8");

        comboBox22.Items.Remove("referenzen");
        comboBox22.Items.Remove("waage1");
        comboBox22.Items.Remove("waage2");
        comboBox22.Items.Remove("waage3");
        comboBox22.Items.Remove("waage4");
        comboBox22.Items.Remove("waage5");
        comboBox22.Items.Remove("waage6");
        comboBox22.Items.Remove("waage7");
        comboBox22.Items.Remove("waage8");

        comboBox23.Items.Remove("referenzen");
        comboBox23.Items.Remove("waage1");
        comboBox23.Items.Remove("waage2");
        comboBox23.Items.Remove("waage3");
        comboBox23.Items.Remove("waage4");
        comboBox23.Items.Remove("waage5");
        comboBox23.Items.Remove("waage6");
        comboBox23.Items.Remove("waage7");
        comboBox23.Items.Remove("waage8");

        comboBox24.Items.Remove("referenzen");
        comboBox24.Items.Remove("waage1");
        comboBox24.Items.Remove("waage2");
        comboBox24.Items.Remove("waage3");
        comboBox24.Items.Remove("waage4");
        comboBox24.Items.Remove("waage5");
        comboBox24.Items.Remove("waage6");
        comboBox24.Items.Remove("waage7");
        comboBox24.Items.Remove("waage8");

        comboBox25.Items.Remove("referenzen");
        comboBox25.Items.Remove("waage1");
        comboBox25.Items.Remove("waage2");
        comboBox25.Items.Remove("waage3");
        comboBox25.Items.Remove("waage4");
        comboBox25.Items.Remove("waage5");
        comboBox25.Items.Remove("waage6");
        comboBox25.Items.Remove("waage7");
        comboBox25.Items.Remove("waage8");

        comboBox26.Items.Remove("referenzen");
        comboBox26.Items.Remove("waage1");
        comboBox26.Items.Remove("waage2");
        comboBox26.Items.Remove("waage3");
        comboBox26.Items.Remove("waage4");
        comboBox26.Items.Remove("waage5");
        comboBox26.Items.Remove("waage6");
        comboBox26.Items.Remove("waage7");
        comboBox26.Items.Remove("waage8");


    }

我认为它应该像(伪代码):

        //Load DB Tables when Drop Down the Dropboxes. And remove not used/needed tables from Combobox.
    private void referenzDropDown(object sender, EventArgs e)
    {
        string myInsertQuery = "SHOW TABLES";
        MySqlCommand myCommand = new MySqlCommand(myInsertQuery, myConnection);
        MySqlDataReader myReader;
        myReader = myCommand.ExecuteReader();

        ActualSelectedComboBox.Items.Clear();

        if (myReader.HasRows == true)
        {
            while (myReader.Read())
            {
                ActualSelectedComboBox.Items.Add((string)myReader[0]);
            }
            myReader.Close();
        }
        ActualSelectedComboBox.Items.Remove("referenzen");
        ActualSelectedComboBox.Items.Remove("waage1");
        ActualSelectedComboBox.Items.Remove("waage2");
        ActualSelectedComboBox.Items.Remove("waage3");
        ActualSelectedComboBox.Items.Remove("waage4");
        ActualSelectedComboBox.Items.Remove("waage5");
        ActualSelectedComboBox.Items.Remove("waage6");
        ActualSelectedComboBox.Items.Remove("waage7");
        ActualSelectedComboBox.Items.Remove("waage8");
    }

有人能告诉我如何做到最好吗? 谢谢!

1 个答案:

答案 0 :(得分:1)

事件处理程序sender参数是使用的组合框。您只需将其解析为ComboBox

即可
private void referenzDropDown(object sender, EventArgs e)
{
    string myInsertQuery = "SHOW TABLES";
    MySqlCommand myCommand = new MySqlCommand(myInsertQuery, myConnection);
    MySqlDataReader myReader;
    myReader = myCommand.ExecuteReader();

    // the "sender" is the control raising this event
    // parse it to ComboBox
    ComboBox actualSelectedComboBox = (ComboBox)sender;

    actualSelectedComboBox.Items.Clear();
    // ...