Dynamically setting a property for a System.Windows.Forms.Control

  • Thread starter Thread starter uiranejeb
  • Start date Start date
U

uiranejeb

Hi everybody,
I have a control that is dragged on a form. Let's say that we have a textbox control named txt1. I want to dynamiclly set a property of this control. I know the name of the control at runtime (not at design time). How can I do this?

Thanks.
 
uiranejeb,

If you don't know anything about the control except the property you
want to set, then you can call the GetProperty method on the Type instance
returned by a call to GetType on the object. Using the PropertyInfo that is
returned, you can call the SetValue method to set the value of the property.

If you know that the control derives from a base class or implements a
common interface which exposes the property, you can cast to that base
class/interface and then set the property through there.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi everybody,
I have a control that is dragged on a form. Let's say that we have a textbox
control named txt1. I want to dynamiclly set a property of this control. I
know the name of the control at runtime (not at design time). How can I do
this?

Thanks.
 
Back
Top