lightswitchr lightswitchr - 2 months ago 10
Vb.net Question

How do I force a SystemIcon to be used as a parameter value?

I have a function which requires a SystemIcon to be passed through as a parameter, however I seem to be unable to specify SystemIcon as the type - I can only specify Icon.

ShowMessage(message As String, icon As Icon) //works

ShowMessage(message As String, icon As SystemIcon) //fails


How can I force the parameter type to be more specific and only allow SystemIcon?

Answer

To expand upon Neolisk's answer, if you view SystemIcons.Application with Intellisense you can see that it is of the type Drawing.Icon.

I recently created a custom message box and ran across the same issue that you are having. The solution I came up with involved creating an enum to match the SystemIcons properties like so:

Public Enum CustomMessageBoxIcon
    Application
    Asterisk
    [Error]
    Exclamation
    Hand
    Information
    Question
    Shield
    Warning
End Enum

This allows me to give Intellisense options on the method, as well as restricting what they can send into the method. Example shown below:

Private Shared Function GetIconFromEnumOptions(ByVal icon As CustomMessageBoxIcon) As Icon
        Select Case icon
            Case CustomMessageBoxIcon.Application
                Return SystemIcons.Application
            Case CustomMessageBoxIcon.Asterisk
                Return SystemIcons.Asterisk
            Case CustomMessageBoxIcon.Error
                Return SystemIcons.[Error]
            Case CustomMessageBoxIcon.Exclamation
                Return SystemIcons.Exclamation
            Case CustomMessageBoxIcon.Hand
                Return SystemIcons.Hand
            Case CustomMessageBoxIcon.Information
                Return SystemIcons.Information
            Case CustomMessageBoxIcon.Question
                Return SystemIcons.Question
            Case CustomMessageBoxIcon.Shield
                Return SystemIcons.Shield
            Case CustomMessageBoxIcon.Warning
                Return SystemIcons.Warning
            Case Else
                Return SystemIcons.WinLogo
        End Select
    End Function
Comments