3

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:

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

Volker
  • 447
  • 1
  • 5
  • 19
  • 2
    You need to make your own form. – SLaks Nov 08 '17 at 21:40
  • 1
    You don't need wasting Your time creating new form at all... look this https://stackoverflow.com/a/235497/3279496 ... the best answer ever... so, instead Yes/No You can write anything You want... and for all other combinations (yes/no/cancel/ignore/ok/.....) – nelek Nov 08 '17 at 22:54

2 Answers2

5

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

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

enter image description here

enter image description here

djv
  • 15,168
  • 7
  • 48
  • 72
2
  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 DialogResult Enum for that.

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:

Form1:

enter image description here

Form2:

enter image description here

Code for 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:

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.

Jonathan Applebaum
  • 5,738
  • 4
  • 33
  • 52