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

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top