Context Menu ForeColor

Amerigo

Newcomer
Joined
Jul 15, 2012
I'm trying to let users change the forecolor of my contextmenu, but not every component has the "ForeColor" property. How to I check for that?

Code:
For Each Itom In MyMenu.SubItems
If Itom.HasProperty("ForeColor") then '<<< This doesn't work.
Itom.ForeColor = MyColor
End If
Next
I also need to be able to dig down to any and all sub-sub items.
 

PlausiblyDamp

Administrator
Joined
Sep 4, 2002
Location
Lancashire, UK
You could always just use a function like
Code:
Module Utils
    Public Function PropertyExists(item As Object, propertyName As String) As Boolean
        Dim pi = item.GetType().GetProperty(propertyName)

        Return Not pi Is Nothing
    End Function
End Module
it could be called like
Code:
Dim test As Boolean
test = Utils.PropertyExists(Me, "ForeColor")
test = Utils.PropertyExists("Hello World", "ForeColor")
test = Utils.PropertyExists(New Button(), "ForeColor")

If you want to be a bit clever about things you could make it into an extension method...
Code:
Module Utils
    <Extension>
    Public Function PropertyExists(item As Object, propertyName As String) As Boolean
        Dim pi = item.GetType().GetProperty(propertyName)

        Return Not pi Is Nothing
    End Function
End Module

That way you could call it from any object e.g.
Code:
Dim test As Boolean
test = Me.PropertyExists("ForeColor")
test = "Hello World".PropertyExists("ForeColor")
test = New Button().PropertyExists("ForeColor")
 
Top Bottom