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 standard dialog
DescriptionThis example shows how to make a standard dialog in Visual Basic 6. It provides a template you can use to handle OK and cancel buttons, and makes the dialog return a success or failure code so it's easy for the main program to process the results.
Keywordsdialog, standard dialog
CategoriesControls, Software Engineering
 
The dialog form has a variable Canceled that indicates whether the user canceled the dialog.

The GetNames function iniitalizes the fields on the dialog, sets Canceled to True, and displays it modally. When the form closes, the function returns vbCancel if the user canceled or vbOK if the user clicked OK.

When the user clicks the Cancel button, the form hides itself. When the user clicks the OK button, the form sets Canceled to False and hides itself.

The Cancel and Default properties of these buttons was set at design time.

 
Private Canceled As Boolean

' Get the values.
Public Function GetNames(ByVal cap As String, ByRef _
    first_name As String, ByRef last_name As String) As _
    Integer
    Caption = cap
    txtLastName.Text = last_name
    txtFirstName.Text = first_name

    ' Assume we will cancel.
    Canceled = True

    ' Display the form.
    Show vbModal

    ' See if the user canceled.
    If Canceled Then
        GetNames = vbCancel
    Else
        GetNames = vbOK
    End If
End Function

Private Sub cmdCancel_Click()
    Hide
End Sub

Private Sub cmdOk_Click()
    Canceled = False
    Hide
End Sub
 
The main form calls the dialog's GetNames function. If that function returns vbOK, then the program gets the form's values.
 
Private Sub cmdSetNames_Click()
    ' Present the dialog and see if the
    ' user pressed Ok.
    If dlgGetNames.GetNames("Enter Names", _
        lblFirstName.Caption, _
        lblLastName.Caption) = vbOK _
    Then
        ' The user pressed Ok.
        ' Get the values from the form.
        lblFirstName.Caption = dlgGetNames.FirstName
        lblLastName.Caption = dlgGetNames.LastName
    End If

    ' Unload the form. IMPORTANT!
    Unload dlgGetNames
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated