0

Can I get the control of an object just knowing its name?

Something like:

Control a;

            a.Name = "ComboBox1";

            ComboBox combo = a as ComboBox;
	
	combo.Items.Add("foo");

Thanks.

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by gusano79
0

Can I get the control of an object just knowing its name?

If you're asking what I think you're asking, no.

Why: Setting the value of a.Name doesn't have anything to do with what type of control it is. a as ComboBox will always return null because here, a is only a Control .

If a is assigned a value that is acutally a ComboBox , then a as ComboBox will be able to get you the actual ComboBox object. Assuming you have a member of the form named comboBox1 , this might be what you want:

Control a;
a = comboBox1;
ComboBox combo = a as ComboBox;
combo.Items.Add("foo");

But in that case, it's easier just use comboBox1 directly.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.