是否可以通过从数组中绘制字符串来设置MessageBoxIcon?
要求: 需要显示一个消息框,其中包含字符串格式的所有必需值。
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))
我遇到了无效的强制转换异常,因为MessageBoxIcon是一个整数,因此String类型无法转换为MessageBoxIcon。有什么方法可以实现这个目标吗?
答案 0 :(得分:2)
MessageBoxButtons和MessageBoxIcon是枚举类型。您可以使用Enum.Parse()方法将字符串值转换为枚举值。我更改了你的字符串,因此它只包含值。
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
答案 1 :(得分:0)
试试这个。
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
答案 2 :(得分:0)
您可以在.NET中使用Dictionary(Of TKey, TValue)
来实现此目的
这将创建一个MessageBox按钮的字典,在这里我使用"OK"
而不是MessageBoxButtons.OK
它更方便或者你改变它
Dim MsgBox_Buttons As New Dictionary(Of String, MessageBoxButtons)
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按钮的字典,在这里我使用Information"
而不是MessageBoxIcon.Information
它更方便或者你改变它
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))
完整代码为here
更多关于 Dictionary()
here