Convert String to Variable Name
I need get combobox by string. Not worked.
for (int i = 0; i < Slots.Count; 开发者_运维百科i++)
{
var field = (ComboBox)this.GetType().GetField("cbSlots" + i).GetValue(this);
field.DataSource = Slots[i.ToString()];
}
If I understand the question, from your form, you could do
this.Controls.Find("name_of_your_combobox");
If you are trying to access a ComboBox
using its name as string, so you need to look for that in this.Controls
like so:
ComboBox myCombo = (ComboBox)this.Controls["cbSlots" + i.ToString()];
精彩评论