467,081 Members | 1,243 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,081 developers. It's quick & easy.

Detect If Control Contains Property

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"
Oct 14 '08 #1
  • viewed: 1217
Share:
2 Replies
On Oct 14, 1:32*pm, "Derek Hart" <derekmh...@yahoo.comwrote:
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/
Oct 14 '08 #2
Derek Hart wrote:
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.
Oct 15 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by vulcaned@isp.com | last post: by
5 posts views Thread by John Kotuby | last post: by
2 posts views Thread by ThunderMusic | last post: by
2 posts views Thread by Derek Hart | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.