使用自定义按钮的简单对话框(VB)类似于MsgBox。

4

我想询问用户一个问题:“你想向左还是向右走?”
为了简化代码,我使用了一个带有提示的MSGBOX:

“你想向左还是向右走?”

按“是”选择“向右”/按“否”选择“向左”

然后我处理被按下的“Yes”/“No”/“Cancel”。这种方法可以运行,但不太美观,在某些情况下很难理解。

此外,在某些情况下,我可能会有超过两个选择,但这可能是另一个问题……


2
你需要制作自己的表单。 - SLaks
1
你根本不需要浪费时间去创建新的表单...看这个 https://dev59.com/WXVC5IYBdhLWcg3woCnN#235497 ... 这是有史以来最好的答案...所以,你可以写任何你想要的东西,而不是只有是/否...对于所有其他组合(是/否/取消/忽略/确定/.....)也同样适用。 - nelek
2个回答

6

您可以动态创建一个

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不同,返回的是字符串,因为现在你可以有无限多的选项!根据您的需要调整大小进行实验。

enter image description here

enter image description here


1
好了,现在大家一起来:「朋友们不能让朋友把‘右’按钮放在左边,把‘左’按钮放在右边!」 - Christian Severin
1
@ChristianSeverin 我成功逃脱了将近6年!:) - djv
1
啊,好吧,既然你这些年来看起来表现得很好,我们就放过你。去吧,我的孩子,不要再犯罪了。 - Christian Severin

2
  1. 根据您的需求创建自己的“自定义”msgbox表单,并最好创建可重用控件-通过构造函数将您的“问题”字符串传递给自定义控件。
  2. 您需要一些“方法”来从自定义 msgbox 之外获取用户决策-其中一种方法是使用 DialogResult 枚举。

这里是我刚写的一个基本示例,以演示,请参见我在代码中添加的注释。


创建一个带有2个窗体的新项目,Form1将是调用自定义msgbox的主窗体,而Form2将是自定义msgbox:

Form1:

enter image description here

Form2:

enter image description here

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技术可以变得更加复杂,但如果你探索这个例子,我希望你能理解其一般思路。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接