Reputation: 433
I have a Method that removes special Items from comboBoxes. At first the comboboxes are filled with table names from a database and then I want to remove some of them that are not needed in the list. I do this by filling and removing the Items of all comboboxes at the same time. But now I want only the comboBox that I actually use beeing filled and items removed on Dropdown. So I think I need to get the name of the actual used comboBox to use it as a variable or somethink like that. How do I do this?
Here is my code until now. You see its very long for just adding and remove Items of a 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");
}
I think it should be something like (pseudocode):
//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");
}
Could someone tell me how to do this best way? Thank you!
Upvotes: 0
Views: 42
Reputation: 43896
The sender
argument to your event handler is the used combobox. You can simply parse it to 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();
// ...
Upvotes: 1