Search for property in a control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hey,

I extended different controls (textbox, combobox,..) with a property.

But now I will find that a control has that property. How can I do this?I
don't want to test if textbox, if combobox. I want to search on every control
(neither which control) if he has that property before I take any action.

if (ctr1.Propery1 == null) this is not correct.

foreach( Control ctr1 in this.groupBox1.Controls)
{
if (ctr1.Propery1 == null) continue;
if (ctr1.Propery1 == "BLA")
{
ctr1.Focus();
break;
}
}

Is there on a control a collection of the properties he has? Can I search in
that collection.
If the control has that property I have to do something.

Tkx,
 
Hi,

you can you type.getproperty function that will return propertyinfo of
property based on property name..

System.Reflection.PropertyInfo pinf =
typeof(TextBox).GetProperty("property_name");
if (pinf != null)
{
// property exist
}

Regards,
Josip Habjan, Croatia
URL: http://www.habjansoftware.com
 
hi,


well you can use reflection as the other posted suggested, the bad thing is
that if you have several controls in your form and/or you do this check
several times it may impact your performance.

Another solution may be declaring an Interface where you define your
property, have the classes you write implement this interface and then you
can use the is operator, like this:

foreach(Control c in Controls)
if( c is IMyInterface )
{
((IMyInterface)c).Property = "XXXXXX";
}


cheers,
 
Have you considered defining an Interface that these controls will
implement. Then you could simply test to see if the control "is"
implementing that interface and if so you'll know that the properties
outlined in the interface are present.
 

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

Back
Top