[英]simple dialog like msgbox with custom buttons (vb)
I want to ask user for example "Do you want to go right or left?". 我想问用户例如“您想向右还是向左走?”。
To have simple Code I use MSGBOX with a prompt like: 为了获得简单的代码,我使用MSGBOX并给出如下提示:
"Do you want to go right or left" “您想向右走还是向左走”
Press "YES for 'right' / NO for 'left'" 按“是”右“ /否”左“”
Then I process Yes/No/Cancel that was pressed. 然后,我处理被按下的是/否/取消。 This works but is ugly and in some cases hard to understand.
这是可行的,但是很难看,在某些情况下很难理解。
Also in Addition in some cases I have more than 2 choices - but that is probable another question... 另外在某些情况下,我还有两个以上的选择-但这可能是另一个问题...
DialogResult
Enum for that. DialogResult
枚举。 Here is a basic example i just wrote to demonstrate that, please see the comments i have added inside the code. 这是我刚刚写来演示的一个基本示例,请参阅我在代码内添加的注释。
create a new project with 2 forms, Form1 will be the main form that will call the custom msgbox and Form2 will be the custom msgbox: 创建一个具有2个表单的新项目,Form1将是将调用自定义msgbox的主表单,而Form2将是自定义msgbox的主表单:
Form1: Form1中:
Form2: 窗体2:
Code for Form1: 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
Code for Form2: 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 can be much more sophisticated but if you explore that example i hope you will understand the general idea. 它可能要复杂得多,但是如果您探索该示例,我希望您能理解总体思路。
You can create one dynamically 您可以动态创建一个
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
You see you have options as to what buttons are present, the message, and title. 您会看到有关存在哪些按钮,消息和标题的选项。
Use it like this 这样使用
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
In my example, the prompt is "Do you want to go right or left?"
在我的示例中,提示是
"Do you want to go right or left?"
and the options are "Right"
and "Left"
. 并且选项为
"Right"
和"Left"
。
The string is returned as opposed to DialogResult because now your options are unlimited (!). 返回字符串,而不是DialogResult,因为现在您的选项是无限的(!)。 Experiment with the size to your suiting.
尝试适合自己的尺码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.