开发者

Convert String to Variable Name

开发者 https://www.devze.com 2023-03-20 19:00 出处:网络
I need get combobox by string. Not worked. for (int i = 0; i < Slots.Count; 开发者_运维百科i++)

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()];
0

精彩评论

暂无评论...
验证码 换一张
取 消