【问题标题】:simple dialog like msgbox with custom buttons (vb)简单的对话框,如带有自定义按钮的 msgbox (vb)
【发布时间】:2017-11-08 21:38:14
【问题描述】:

我想问用户,例如“你想向右走还是向左走?”。
为了获得简单的代码,我使用 MSGBOX 并带有如下提示:

“你要向右还是向左”

“右”按“是”/“左”按“否”

然后我处理按下的是/否/取消。这可行,但很丑陋,在某些情况下难以理解。

此外,在某些情况下,我有两个以上的选择 - 但这可能是另一个问题......

【问题讨论】:

  • 您需要自己制作表格。
  • 您根本不需要浪费时间创建新表单...看看这个stackoverflow.com/a/235497/3279496...有史以来最好的答案...所以,是/否您可以写任何东西想要...以及所有其他组合(是/否/取消/忽略/确定/.....)

标签: vb.net


【解决方案1】:

你可以动态创建一个

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

在我的示例中,提示符为"Do you want to go right or left?",选项为"Right""Left"

返回字符串而不是 DialogResult,因为现在您的选项是无限的 (!)。尝试适合您的西装尺寸。

【讨论】:

    【解决方案2】:
    1. 您需要根据自己的需要创建自己的“自定义”msgbox 表单,并且最好创建一个可重用的控件 - 通过自定义控件的构造函数传递您的“问题”字符串。
    2. 您需要一些“方法”来从您的自定义 msgbox 之外获取用户决定 - 一种方法是使用 DialogResult Enum 来实现。

    这是我刚刚编写的一个基本示例来演示这一点,请参阅我在代码中添加的 cmets。


    创建一个包含 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
    

    它可以更复杂,但如果你研究这个例子,我希望你能理解大体的想法。

    【讨论】:

      猜你喜欢
      • 2012-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-16
      • 1970-01-01
      相关资源
      最近更新 更多