Get control form its NAME
-
Hi, I get the name of the control in string format in an event: "control1" And I want to modify its properties (for ex.: control1.BackColor = Color.White; How can I convert or point this string to the existing instance of the control?? regards Radgar "Imagination is more important than knowledge." - Albert Einstein
-
Hi, I get the name of the control in string format in an event: "control1" And I want to modify its properties (for ex.: control1.BackColor = Color.White; How can I convert or point this string to the existing instance of the control?? regards Radgar "Imagination is more important than knowledge." - Albert Einstein
Reflection. Christian Graus - Microsoft MVP - C++
-
Reflection. Christian Graus - Microsoft MVP - C++
-
thanks Christian but it's a huge namespace for me to solve this issue right now. I tried PropertyInfo and checked the MemberInfo classes but still found no answers. Radgar "Imagination is more important than knowledge." - Albert Einstein
You're saying that reflection is a huge space to learn, or that you're working in a huge namespace ? Christian Graus - Microsoft MVP - C++
-
You're saying that reflection is a huge space to learn, or that you're working in a huge namespace ? Christian Graus - Microsoft MVP - C++
I meant the Reflection namespace is huge but still I need to learn that. It's just that it's 5 am and I have a little time to finish what I'm writing. that's all. Anyway, thanks for the help.. I think I should use MemberInfo class and tried smth like this:
System.Reflection.MemberInfo[] myMembers = ....GetType().GetMember("pictureBox1"); this.Controls.Remove(myMembers[0]);
on it.. Radgar "Imagination is more important than knowledge." - Albert Einstein -
I meant the Reflection namespace is huge but still I need to learn that. It's just that it's 5 am and I have a little time to finish what I'm writing. that's all. Anyway, thanks for the help.. I think I should use MemberInfo class and tried smth like this:
System.Reflection.MemberInfo[] myMembers = ....GetType().GetMember("pictureBox1"); this.Controls.Remove(myMembers[0]);
on it.. Radgar "Imagination is more important than knowledge." - Albert EinsteinNo, I don't think that's the way to go. Sorry, I don't know a lot more, but definately reflection is the only way you'll get variables out of names, unless you build a hash table before you start and look it up. Christian Graus - Microsoft MVP - C++
-
No, I don't think that's the way to go. Sorry, I don't know a lot more, but definately reflection is the only way you'll get variables out of names, unless you build a hash table before you start and look it up. Christian Graus - Microsoft MVP - C++
-
Hi, I get the name of the control in string format in an event: "control1" And I want to modify its properties (for ex.: control1.BackColor = Color.White; How can I convert or point this string to the existing instance of the control?? regards Radgar "Imagination is more important than knowledge." - Albert Einstein
Well friend I don't have fancy solution like reflection but this should do the trick... I bet 90% of people are using it... hey -> not all of us are MVPs ;)... NHF? I'm just kidding ;)
foreach (Control c in this.Controls) { if (c.Name == "control1") { // do smthing like c.BackColor = Color.White } }
Now if ur's event isn't in form which control u need to modify, than you'll need to pass that form, or it's collection Controls. Drop a line if u need more hints... -
Well friend I don't have fancy solution like reflection but this should do the trick... I bet 90% of people are using it... hey -> not all of us are MVPs ;)... NHF? I'm just kidding ;)
foreach (Control c in this.Controls) { if (c.Name == "control1") { // do smthing like c.BackColor = Color.White } }
Now if ur's event isn't in form which control u need to modify, than you'll need to pass that form, or it's collection Controls. Drop a line if u need more hints... -
Hi, I get the name of the control in string format in an event: "control1" And I want to modify its properties (for ex.: control1.BackColor = Color.White; How can I convert or point this string to the existing instance of the control?? regards Radgar "Imagination is more important than knowledge." - Albert Einstein
you may use reflection to get the type by name and then call InvokeMember
-
Hi, I get the name of the control in string format in an event: "control1" And I want to modify its properties (for ex.: control1.BackColor = Color.White; How can I convert or point this string to the existing instance of the control?? regards Radgar "Imagination is more important than knowledge." - Albert Einstein
Not sure if this'll compile, but I've done something similar to this:
string controlname = "control1"; string propertyname = "BackColor"; object newvalue = Color.Black; Control control; control = form1.GetType().InvokeMember( controlname, BindingFlags.Instance|BindingFlags.GetField, null, form1, new object[] {} ); control.GetType().InvokeMember( propertyname, BindingFlags.Instance|BindingFlags.SetProperty, null, control, new object[] { newvalue } );
-- Joel Lucsy