获取要在方法C#WinForms中使用的实际使用的GUI元素(comboBox)的名称
我有一个从组合框中删除特殊项的方法。 首先,组合框由数据库中的表名填充,然后我想删除列表中不需要的一些表名。 我通过同时填充和删除所有组合框的项目来实现这一点。但现在我只想要我实际使用的组合框,在下拉列表中填充和删除项目。所以我想我需要得到实际使用的组合框的名称,以便将其用作变量或类似的东西。我该怎么做 这是我到目前为止的代码。您可以看到,仅仅添加和删除组合框中的项目是非常漫长的获取要在方法C#WinForms中使用的实际使用的GUI元素(comboBox)的名称,c#,winforms,C#,Winforms,我有一个从组合框中删除特殊项的方法。 首先,组合框由数据库中的表名填充,然后我想删除列表中不需要的一些表名。 我通过同时填充和删除所有组合框的项目来实现这一点。但现在我只想要我实际使用的组合框,在下拉列表中填充和删除项目。所以我想我需要得到实际使用的组合框的名称,以便将其用作变量或类似的东西。我该怎么做 这是我到目前为止的代码。您可以看到,仅仅添加和删除组合框中的项目是非常漫长的 //Load DB Tables when Drop Down the Dropboxes. And
//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");
}
我认为应该是(伪代码):
有人能告诉我怎样才能做到最好吗?
谢谢大家! 事件处理程序的
sender
参数是已使用的组合框。您只需将其解析为组合框:
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();
// ...
每个UI元素都应该有相关的
标记
属性。对于那个特定的组合,您可能会分配一些特殊的东西给您,这在语义上是有意义的。在那之后,找到它只是一个查询问题:form.Controls.Where(c=>c.Tag!=null&&c.Tag.ToString()=“您的密钥”)作为组合框!这对我有帮助!
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();
// ...