Detect If Control Contains Property

  • Thread starter Thread starter Derek Hart
  • Start date Start date
D

Derek Hart

How can I detect if a control contains a property. Need to use reflection
somehow?

If ctl.ContainsProperty("Tag") Then ctl.Tag = "some tag information"
 
How can I detect if a control contains a property. Need to use reflection
somehow?

If ctl.ContainsProperty("Tag") Then ctl.Tag = "some tag information"

Yes, you can use the <Type>.GetProperty(...) to determine if a
property exists and also to set the value there. If however you are
just interested with controls, it would be simpler to see if the
object is a control, and then simply cast it into a control. Then the
tag property would be available as normal.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
Derek said:
How can I detect if a control contains a property.

Why do you need to?

You can /ask/ any Control (or, indeed, any Object) what Type it is, and
then process it accordingly, as in

Sub Click( sender as Object e as EventArgs )

If Typeof sender is CheckBox Then
With DirectCast( sender, CheckBox )
.Text = .Text.ToUpper()
.CheckState = CheckedState.Checked
End With

Else If Typeof sender is TextBox Then
With DirectCast( sender, TextBox )
.Text = .Text.ToLower()
End With

End If
End Sub
Need to use reflection somehow?

If you /really/ must, but it's more difficult and (I would expect) quite
a bit slower as well.

HTH,
Phill W.
 
Back
Top