【问题标题】:How to set messagebox icon using a string variable in vb.net如何在 vb.net 中使用字符串变量设置消息框图标
【发布时间】:2020-02-29 23:23:35
【问题描述】:

我一直试图让一个程序打开一个带有由字符串变量标识的图标的消息框,但我不知道怎么做!我试过用这样的东西

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

但它给了我错误: 'MessageBoxIcon' 是一种类型,不能用作表达式。

【问题讨论】:

  • Why 你想让IconVariable 成为一个字符串吗?
  • 您正在尝试做一些从表面上看毫无意义的事情。如果您有正当理由,则由您自行解释。您始终需要对问题提供完整且清晰的解释。

标签: vb.net visual-studio-2010


【解决方案1】:

我根据@GSerg 在 cmets 中建议的链接创建了一个函数。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多