Convert String to MessageBoxIcon
是否可以通过从数组中绘制字符串来设置MessageBoxIcon?
要求:需要显示一个消息框,其中我具有所有必需的字符串格式值。
1 2 3 4 5 6 | Dim MessageTitle as String ="Procedure is starting in 10 seconds" Dim MessageBody as String ="Some description here" Dim MessageIcon as String ="MessageBoxIcon.Information" Dim MessageButtons as String ="MessageBoxButtons.OK" MessageBox.Show(MessageBody, MessageTitle, Ctype(MessageButtons, MessageBoxButtons), Ctype(MessageIcon, MessageBoxIcon)) |
I被卡在无效的强制转换异常中,因为MessageBoxIcon是整数,因此无法将String类型转换为MessageBoxIcon。有什么方法可以实现这一目标?
MessageBoxButtons和MessageBoxIcon是枚举类型。您可以使用Enum.Parse()方法将字符串值转换为枚举值。我更改了您的字符串,使其仅包含值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Public Class Form1 Dim MessageTitle As String ="Procedure is starting in 10 seconds" Dim MessageBody As String ="Some description here" Dim MessageIcon As String ="Information" Dim MessageButtons As String ="OKCancel" Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim msgButton As MessageBoxButtons = [Enum].Parse(GetType(MessageBoxButtons), MessageButtons) Dim msgIcon As MessageBoxIcon = [Enum].Parse(GetType(MessageBoxIcon), MessageIcon) MessageBox.Show(MessageBody, MessageTitle, msgButton, msgIcon) End Sub End Class |
您可以在.NET中使用
这将创建一个MessageBox Buttons字典,在这里,我使用
2
3
4
5
6
7
8
9
With MsgBox_Buttons
.Add("OK", MessageBoxButtons.OK)
.Add("OKCancel", MessageBoxButtons.OKCancel)
.Add("AbortRetryIgnore", MessageBoxButtons.AbortRetryIgnore)
.Add("RetryCancel", MessageBoxButtons.RetryCancel)
.Add("YesNo", MessageBoxButtons.YesNo)
.Add("YesNoCancel", MessageBoxButtons.YesNoCancel)
End With
这将创建一个MessageBox Buttons字典,在这里我使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Dim MsgBox_Icons As New Dictionary(Of String, MessageBoxIcon) With MsgBox_Icons .Add("Asterisk", MessageBoxIcon.Asterisk) .Add("Error", MessageBoxIcon.Error) .Add("Exclamation", MessageBoxIcon.Exclamation) .Add("Hand", MessageBoxIcon.Hand) .Add("Information", MessageBoxIcon.Information) .Add("None", MessageBoxIcon.None) .Add("Question", MessageBoxIcon.Question) .Add("Stop", MessageBoxIcon.Stop) .Add("Warning", MessageBoxIcon.Warning) End With Dim MessageTitle As String ="Procedure is starting in 10 seconds" Dim MessageBody As String ="Some description here" Dim MessageIcon As String ="Information" 'note this change (You can alter it but the change should be don in the dictionary also) Dim MessageButtons As String ="OK" 'note this change(You can alter it but the change should be don in the dictionary also) MessageBox.Show(MessageBody, MessageTitle, MsgBox_Buttons.Item(MessageButtons), MsgBox_Icons.Item(MessageIcon)) |
完整代码在这里
关于
尝试一下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load AddIcons() AddButtons() ShowIConz(1, 1) End Sub Public Sub AddIcons() MIcon.Add(MessageBoxIcon.Asterisk) '0 MIcon.Add(MessageBoxIcon.Error) '1 MIcon.Add(MessageBoxIcon.Exclamation) '2 MIcon.Add(MessageBoxIcon.Hand) '3 MIcon.Add(MessageBoxIcon.Information) '4 MIcon.Add(MessageBoxIcon.None) '5 MIcon.Add(MessageBoxIcon.Question) '6 MIcon.Add(MessageBoxIcon.Stop) '7 MIcon.Add(MessageBoxIcon.Warning) '8 End Sub Public Sub AddButtons() MButton.Add(MessageBoxButtons.AbortRetryIgnore) '0 MButton.Add(MessageBoxButtons.OK) '1 MButton.Add(MessageBoxButtons.OKCancel) '2 MButton.Add(MessageBoxButtons.RetryCancel) '3 MButton.Add(MessageBoxButtons.YesNo) '4 MButton.Add(MessageBoxButtons.YesNoCancel) '5 End Sub Public Sub ShowIConz(iconnumber As Double, buttonnumber As Double) Dim MessageTitle As String ="Procedure is starting in 10 seconds" Dim MessageBody As String ="Some description here" UseIcon = MIcon(iconnumber) UseButton = MButton(buttonnumber) MessageBox.Show(MessageBody, MessageTitle, UseButton, UseIcon) End Sub |