I have a situation, don’t know if my approach is right or not please guide me through this.
Suppose I have a Panel control containing numerous controls in it,
During runtime I performed an iteration over each control in that panel by using
Panel1.Controls property,
Now in those controls they could be anything TextBox, Button, DropDown etc.
Now I want to find during runtime, which Controls is of which type and after that find if any specific property is contained in that control or not & if that property exists there than set the value of that property.
I think I would have to do some thing using Reflection here but don’t know from where to start.
sample code:
foreach (Control cntrl in Panel1.Controls)
{
//find type of the control
// find any specific property's existence in that control
// if property exists than set value of that property
}
Any other more relevant approach is welcomed too, for performing this task in runtime.
Sorry I forgot to mention
I don’t want to use is keyword here because controls are of may types, and I want to create a global function which can be used for any panel with out knowing the types of controls present in that panel.
Thankx in advance.
By using reflection you can achieve this.
Get the metadata for the property, and then set it. This will allow you to check for the existence of the property, and verify that it can be set:Or try this if you do not want to use reflection
Note: is keyword Checks if an object is compatible with a given type. For example, the following code can determine if an object is an instance of the MyObject type, or a type that derives from MyObject: