0

I've been trying to make a program open a messagebox with the icon identified by a string variable, but I can't figure out how! I've tried using something like this

MessageBox.Show("Message here", _
    "Message", _
    MessageBoxButtons.YesNoCancel, _
    MessageBoxIcon. + IconVariable)

But it gives me the error: 'MessageBoxIcon' is a type and cannot be used as an expression.

  • [Why](https://meta.stackexchange.com/q/66377/147640) do you want `IconVariable` to be a string specifically? – GSerg Feb 29 '20 at 23:29
  • 2
    Possible duplicate of [Parse a string to an Enum value in VB.NET](https://stackoverflow.com/questions/852141/parse-a-string-to-an-enum-value-in-vb-net) – GSerg Feb 29 '20 at 23:30
  • You're trying to do something that, on the face of it, makes no sense. If you have a legitimate reason, it's up to you to explain that. You ALWAYS need to provide a FULL and CLEAR explanation of the problem. – jmcilhinney Mar 01 '20 at 02:43

1 Answers1

0

I created a Function from the link suggested by @GSerg in comments.

Private Function GetIconEnum(s As String) As MessageBoxIcon
    Dim icon As MessageBoxIcon
    Try
        icon = DirectCast([Enum].Parse(GetType(MessageBoxIcon), s), MessageBoxIcon)
    Catch ex As Exception
        Return MessageBoxIcon.None
    End Try
    Return Icon
End Function

Private Sub OPCode()
    Dim IconVariable As String = "Question"
    MessageBox.Show("Message here", "Message", MessageBoxButtons.YesNoCancel, GetIconEnum(IconVariable))
End Sub
Mary
  • 14,926
  • 3
  • 18
  • 27