我想询问用户一个问题:“你想向左还是向右走?”
为了简化代码,我使用了一个带有提示的MSGBOX:
“你想向左还是向右走?”
按“是”选择“向右”/按“否”选择“向左”
然后我处理被按下的“Yes”/“No”/“Cancel”。这种方法可以运行,但不太美观,在某些情况下很难理解。
此外,在某些情况下,我可能会有超过两个选择,但这可能是另一个问题……
我想询问用户一个问题:“你想向左还是向右走?”
为了简化代码,我使用了一个带有提示的MSGBOX:
“你想向左还是向右走?”
按“是”选择“向右”/按“否”选择“向左”
然后我处理被按下的“Yes”/“No”/“Cancel”。这种方法可以运行,但不太美观,在某些情况下很难理解。
此外,在某些情况下,我可能会有超过两个选择,但这可能是另一个问题……
您可以动态创建一个
Public Module CustomMessageBox
Private result As String
Public Function Show(options As IEnumerable(Of String), Optional message As String = "", Optional title As String = "") As String
result = "Cancel"
Dim myForm As New Form With {.Text = title}
Dim tlp As New TableLayoutPanel With {.ColumnCount = 1, .RowCount = 2}
Dim flp As New FlowLayoutPanel()
Dim l As New Label With {.Text = message}
myForm.Controls.Add(tlp)
tlp.Dock = DockStyle.Fill
tlp.Controls.Add(l)
l.Dock = DockStyle.Fill
tlp.Controls.Add(flp)
flp.Dock = DockStyle.Fill
For Each o In options
Dim b As New Button With {.Text = o}
flp.Controls.Add(b)
AddHandler b.Click,
Sub(sender As Object, e As EventArgs)
result = DirectCast(sender, Button).Text
myForm.Close()
End Sub
Next
myForm.FormBorderStyle = FormBorderStyle.FixedDialog
myForm.Height = 100
myForm.ShowDialog()
Return result
End Function
End Module
您可以选择显示哪些按钮、消息和标题。
使用方法如下:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim result = CustomMessageBox.Show(
{"Right", "Left"},
"Do you want to go right or left?",
"Confirm Direction")
MessageBox.Show(result)
End Sub
End Class
DialogResult
枚举。这里是我刚写的一个基本示例,以演示,请参见我在代码中添加的注释。
创建一个带有2个窗体的新项目,Form1将是调用自定义msgbox的主窗体,而Form2将是自定义msgbox:
Form1:
Form2:
Form1的代码:
Public Class Form1
Private Sub btnOpenCustomMsgbox_Click(sender As Object, e As EventArgs) Handles btnOpenCustomMsgbox.Click
Dim customMsgbox = New Form2("this is my custom msg, if you press yes i will do something if you press no i will do nothing")
If customMsgbox.ShowDialog() = DialogResult.Yes Then
' do something
MsgBox("I am doing some operation...")
Else
' do nothing (its DialogResult.no)
MsgBox("I am doing nothing...")
End If
End Sub
End Class
Form2的代码:
Public Class Form2
' field that will contain the messege
Private PromtMsg As String
Sub New(ByVal promtmsg As String)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
' set global field with the argument that was passed to the constructor
Me.PromtMsg = promtmsg
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' set the msg label
Me.lblPromtMsg.Text = Me.PromtMsg
End Sub
Private Sub btnCustomYes_Click(sender As Object, e As EventArgs) Handles btnCustomYes.Click
' user choosed yes - return DialogResult.Yes
Me.DialogResult = DialogResult.Yes
Me.Close()
End Sub
Private Sub btnCustomNo_Click(sender As Object, e As EventArgs) Handles btnCustomNo.Click
' user choosed no - DialogResult.no
Me.DialogResult = DialogResult.No
Me.Close()
End Sub
End Class
IT技术可以变得更加复杂,但如果你探索这个例子,我希望你能理解其一般思路。