I wrote this method so i can use it instead of repeating the code.but how can i change cmb1
(ComboBox Name) to something like cmb(i)
. what i mean if i =10 ComboBox name = cmb10
.for this part of the code(cmb1.Items.Add(srd.GetValue(0).ToString());).i want to change the number regarding to i.is it possible?if it is please help me!
This is the method im trying
public void ComboText(string text,int i)
{
try
{
string connectionString = @"Data Source=DESKTOP-V0HE9JH\SQLEXPRESS01;Initial Catalog=Parts_Shop;Integrated Security=True";
SqlConnection connection = new SqlConnection(@connectionString);
connection.Open();
SqlCommand command = new SqlCommand("select manufacturer from Parts where item_type = '" + text + "'", connection);
SqlDataReader srd = command.ExecuteReader();
while (srd.Read())
{
cmb1.Items.Add(srd.GetValue(0).ToString());
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
The Controls
collection has two indexer overloads one accepting an int
and another a string
representing the name of the control.
var cmb = (ComboBox)Controls["cmb" + i];
This works if the combo box is placed directly on the form. If it is placed on another control like e.g. a panel, then access this container control's Controls
property:
var cmb = (ComboBox)panel1.Controls["cmb" + i];