Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a simple standard dialog in VB .NET
DescriptionThis example shows how to make a simple standard dialog in VB .NET.
KeywordsBarnsley's Fern, fractal, iterated functions, iterated function system, VB.NET
CategoriesSoftware Engineering
 
At design time, set the dialog's AcceptButton property to the OK button and set its CancelButton property to the Cancel button. When the user clicks the CancelButton, the form automatically sets its DialogResult property to DialogResult.Cancel. That makes the form hide and makes the ShowDialog method return DialogResult.Cancel.

In the OK button's Click event handler, validate the user's input and, if everything is okay, set the form's DialogResult property to DialogResult.OK. That automatically hides the form and sets the ShowDialog functions return value to DialogResult.OK.

 
' Verify that the names are non-blank.
Private Sub btnOk_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnOk.Click
    If txtFirstName.Text.Length < 1 Then
        MessageBox.Show("First Name must not be blank", _
            "Invalid Value", _
            MessageBoxButtons.OK, _
            MessageBoxIcon.Exclamation)
        txtFirstName.Focus()
        Exit Sub
    End If
    If txtLastName.Text.Length < 1 Then
        MessageBox.Show("Last Name must not be blank", _
            "Invalid Value", _
            MessageBoxButtons.OK, _
            MessageBoxIcon.Exclamation)
        txtLastName.Focus()
        Exit Sub
    End If

    ' Set the dialog's DialogResult. 
    ' This automatically hides the dialog.
    DialogResult = DialogResult.OK
End Sub
 
This example creates property procedures for each of the values it will display on the dialog. In this case, it's just FirstName and LastName strings. You could make procedures to let the program get and set objects with their own properties if you like.
 
' Properties that the dialog edits.
Property FirstName() As String
    Get
        Return txtFirstName.Text
    End Get
    Set(ByVal Value As String)
        txtFirstName.Text = Value
    End Set
End Property
Property LastName() As String
    Get
        Return txtLastName.Text
    End Get
    Set(ByVal Value As String)
        txtLastName.Text = Value
    End Set
End Property
 
This example shadows the Show method so the program cannot display the dialog by calling Show. Instead it must call ShowDialog.
 
' Do not allow the program to call the dialog's Show method.
Public Shadows Sub Show()
    Throw New InvalidOperationException("Use ShowDialog not " & _
        "Show to display this dialog")
End Sub
 
The following code shows how the main program can use the dialog. When you click the Show Dialog button, the program creates a dialog and initializes its FirstName and LastName properties. It calls the dialog's ShowDialog method and, if the method returns DialogResult.OK, the program updates its m_FirstName and m_LastName values. It displays the result in the form's caption.
 
Private m_FirstName As String = ""
Private m_LastName As String = ""

Private Sub btnShowDialog_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnShowDialog.Click
    Dim dlg As New dlgName
    dlg.FirstName = m_FirstName
    dlg.LastName = m_LastName

    'If dlg.ShowDialog("<first>", "<last>") =
    ' DialogResult.OK Then
    If dlg.ShowDialog() = DialogResult.OK Then
        m_FirstName = dlg.FirstName
        m_LastName = dlg.LastName

        Me.Text = m_FirstName & " " & m_LastName
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated