vb.net 简单的对话框,如带有自定义按钮的 msgbox (vb)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47190131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:21:17  来源:igfitidea点击:

simple dialog like msgbox with custom buttons (vb)

vb.net

提问by Volker

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...

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

回答by jonathana

  1. You need to create your own "custom" msgbox form according to your needs, and its better to create a reusable control - pass your "question" string via the constructor of your custom control.
  2. You need some "way" to get the user decision from out side your custom msgbox - one way is use DialogResultEnum for that.
  1. 您需要根据需要创建自己的“自定义”msgbox 表单,最好创建一个可重用的控件 - 通过自定义控件的构造函数传递您的“问题”字符串。
  2. 您需要一些“方式”来从您的自定义 msgbox 外部获得用户决定 - 一种方法是使用DialogResultEnum 。

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:

表格1:

enter image description here

在此处输入图片说明

Form2:

表格2:

enter image description here

在此处输入图片说明

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.

它可以更复杂,但如果你探索那个例子,我希望你能理解一般的想法。

回答by djv

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?"and the options are "Right"and "Left".

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

The string is returned as opposed to DialogResult because now your options are unlimited (!). Experiment with the size to your suiting.

返回字符串而不是 DialogResult 因为现在您的选项是无限的 (!)。试穿适合您的尺码。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明