Overview







Many times when using the InputBox I've found it to be lacking in functionality with it not being a true Dialog. This is something most of the more experienced programmers online agree on, but you often see homework assignments that specify using an InputBox.

This example encapsulates the creation of a DialogBox that has the appearance and functionality of an InputBox but also the functionality of a modal DialogBox, returning either DialogResult.OK or DialogResult.Cancel.
The code used to call it is as you would call a Dialog, and if the result is DialogResult.OK, the returnedValue Property contains the Text the User entered.

Compared to a standard InputBox, the InputDialog takes a few more lines of code to utilize it in your application than the single line InputBox, but gives you far more control over what is entered and retrieved from the Dialog.

As it is, the InputDialog is a fully reusable Class that encapsulates everything necessary to use the Dialog. There are no additional Forms or Resources. All you need to do is add the class to your project and call it as shown in the example.



Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim d As New InputDialog
        'this shows a modal Dialog with a prompt, no title, and a DefaultResponse
        If d.ShowDialog("Enter a name", , "test") = Windows.Forms.DialogResult.OK Then
            'this is the input returned by the Dialog
            MsgBox(d.returnedValue)
        End If
    End Sub
 
End Class









The Dialog Code





Public Class InputDialog
 
    ''' <summary>
    ''' returnedValue Property
    ''' </summary>
    ''' <remarks>Used for retrieving the text entered in the Dialog</remarks>
    Private _returnedValue As String = ""
    Public ReadOnly Property returnedValue() As String
        Get
            Return _returnedValue
        End Get
    End Property
 
    ''' <summary>
    ''' Private Class level Variables
    ''' </summary>
    ''' <remarks></remarks>
    Private prompt As String
    Private defaultResponse As String
 
    Private frm As Form
    Private txt As TextBox
    Dim btn1 As Button
 
    ''' <summary>
    ''' ShowDialog Function
    ''' </summary>
    ''' <param name="Prompt">The Prompt shown in the InputDialog</param>
    ''' <param name="Title">The Title for the InputDialog (Optional)</param>
    ''' <param name="DefaultResponse">The DefaultResponse for the InputDialog (Optional)</param>
    ''' <param name="XPos">The Left position of the InputDialog (Optional)</param>
    ''' <param name="YPos">The Top position of the InputDialog (Optional)</param>
    ''' <returns>A DialogResult (OK or Cancel)</returns>
    ''' <remarks>This creates an InputBox that has the characteristics of a DialogBox.
    ''' '''''''''Everything needed to use this InputDialog is encapsulated in this class.</remarks>
    Public Function ShowDialog(ByVal Prompt As String, Optional ByVal Title As String = "", Optional ByVal DefaultResponse As String = "", Optional ByVal XPos As Integer = -1, Optional ByVal YPos As Integer = -1) As DialogResult
        Me.prompt = Prompt
        Me.defaultResponse = DefaultResponse
 
        'this is the Form that initiated this Dialog
        Dim a As Form = Form.ActiveForm
        'this is the screen containing the Form that initiated this Dialog
        Dim s As Screen = If(a Is Nothing, Screen.PrimaryScreen, Screen.AllScreens.First(Function(scr) scr.Bounds.Contains(a.Bounds)))
 
        'these are the coordinates the InputDialog will be placed at
        'default is center of the screen containing the Form that initiated this Dialog
        XPos = If(XPos = -1, s.Bounds.X + s.Bounds.Width \ 2 - 185, XPos)
        YPos = If(YPos = -1, s.Bounds.Y + s.Bounds.Height \ 2 - 82, YPos)
 
        'this creates the Dynamic Dialog Form
        frm = New Form With { _
                .Text = If(Title <> "", Title, Reflection.Assembly.GetExecutingAssembly().GetName().Name), _
                .StartPosition = FormStartPosition.Manual, _
                .Size = New Size(370, 165), _
                .FormBorderStyle = FormBorderStyle.FixedDialog, _
                .Location = New Point(XPos, YPos), _
                .MinimizeBox = False, _
                .MaximizeBox = False}
 
        'this is the OK button that is added to the Form
        btn1 = New Button With { _
                .Text = "OK", _
                .Size = New Size(50, 25), _
                .Location = New Point(300, 10), _
                .DialogResult = DialogResult.OK, _
                .Enabled = False}
        frm.Controls.Add(btn1)
        frm.AcceptButton = btn1
 
        'this is the Cancel button that is added to the Form
        Dim btn2 As New Button With { _
                .Text = "Cancel", _
                .Size = New Size(50, 25), _
                .Location = New Point(300, 40)}
        frm.Controls.Add(btn2)
 
        AddHandler btn2.Click, AddressOf btn2_Click
 
        'this is the TextBox that is added to the Form
        txt = New TextBox With { _
        .Size = New Size(330, 22), _
        .Location = New Point(20, 100), _
        .Text = DefaultResponse}
        frm.Controls.Add(txt)
        btn1.Enabled = DefaultResponse <> ""
 
        _returnedValue = DefaultResponse
 
        AddHandler txt.TextChanged, AddressOf txt_TextChanged
        AddHandler frm.Paint, AddressOf frm_Paint
        AddHandler frm.Shown, AddressOf frm_Shown
 
        'Return value to the calling Method is the DialogResult returned by the Form
        Return frm.ShowDialog
    End Function
 
    ''' <summary>
    ''' Dynamic Form Cancel Button Click event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Ensures no value will be returned if cancelled, and closes Dialog Form</remarks>
    Private Sub btn2_Click(ByVal sender As Object, ByVal e As EventArgs)
        _returnedValue = ""
        frm.DialogResult = DialogResult.Cancel
    End Sub
 
    ''' <summary>
    ''' Dynamic Form TextBox TextChanged event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Writes the TextBox.Text to the returnedValue Property for retrieval after the Dialog is closed</remarks>
    Private Sub txt_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
        _returnedValue = txt.Text
        btn1.Enabled = txt.Text <> ""
    End Sub
 
    ''' <summary>
    ''' Dynamic Form Paint event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Renders the Prompt value on the Form</remarks>
    Private Sub frm_Paint(ByVal sender As Object, ByVal e As PaintEventArgs)
        Dim r As New Rectangle(20, 10, 240, 80)
        e.Graphics.DrawString(Me.prompt, frm.Font, Brushes.Black, r, Drawing.StringFormat.GenericTypographic)
    End Sub
 
    ''' <summary>
    ''' Dynamic Form Shown event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>If DefaultResponse is specified, it will appear selected</remarks>
    Private Sub frm_Shown(ByVal sender As Object, ByVal e As EventArgs)
        txt.SelectAll()
        txt.Focus()
    End Sub
 
End Class



Other Resources



Download here...
Download V2 here...

VBForums CodeBank versions...