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.
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.
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.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.