How to use the DialogResult property in Visual Basic .NET or in Visual Basic 2005

Summary

This article demonstrates how to use the DialogResult property in Windows Forms. You can use the DialogResult property to create dialog boxes in Windows applications.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
This article assumes that you are familiar with the following topics:
  • Windows applications
  • Microsoft Visual Basic .NET or Microsoft Visual Basic 2005

Using the DialogResult Property

This sample uses the DialogResult property to determine which button a user clicks to close a form.

  1. Create a new Windows Application project in Visual Basic .NET. Form1 is added to the project by default.

    Note You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-generated code from being interspersed with your code.

    For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site:

    For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:

  2. In the Design window of Form1, add a Button control. Change the Name property to btnShowForm2, and then change the Text property to Show Form2.
  3. On the Project menu, click Add Windows Form to add a new form to this project. You will use this form as a dialog box.
  4. Click Open. A new form named Form2 is created.
  5. Add two Button controls to Form2. Button1 and Button2 are added by default. Set the properties of these buttons as follows:Button1

    • Name: btnOK
    • DialogResult: OK
    • Text: OK

    Button2

    • Name: btnCancel
    • DialogResult: Cancel
    • Text: Cancel

  6. Form2 is complete. Notice that you completed these steps through the Windows Forms Designer and the Properties window. You did not copy or write any code.
  7. Return to the Design window of Form1. Double-click the btnShowForm2 control. The Visual Studio .NET Integrated Development Environment (IDE) opens the Code window of Form1 and automatically creates the function prototype for the btnShowForm2_Click event:
    Private Sub btnShowForm2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnShowForm2.Click

    End Sub
  8. Add the following code to the btnShowForm2_Click event procedure:
    Dim dr As DialogResult
    Dim f As New Form2()

    dr = f.ShowDialog()
    If dr = DialogResult.OK Then
    MsgBox("User clicked OK button")
    ElseIf dr = DialogResult.Cancel Then
    MsgBox("User clicked Cancel button")
    End If
This displays Form2 as a modal dialog box. When the user clicks either OK or Cancel, a message box displays which button the user clicked.

Complete Code Listing

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'The Windows Form Designer requires this call.
InitializeComponent()

'Add any initialization after the InitializeComponent() call.

End Sub

'Form overrides Dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Friend WithEvents btnShowForm2 As System.Windows.Forms.Button

'The Windows Form Designer requires this code.
Private components As System.ComponentModel.Container

'NOTE: The Windows Form Designer requires this procedure.
'You can use the Windows Form Designer to modify the code.
'However, do not use the Code editor to modify it.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnShowForm2 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'Button1
'
Me.btnShowForm2.Location = New System.Drawing.Point(112, 88)
Me.btnShowForm2.Name = "Button1"
Me.btnShowForm2.TabIndex = 0
Me.btnShowForm2.Text = "Show Form2"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(288, 165)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub btnShowForm2_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnShowForm2.Click
Dim dr As DialogResult
Dim f As New Form2()

dr = f.ShowDialog()
If dr = DialogResult.OK Then
MsgBox("User clicked OK button")
ElseIf dr = DialogResult.Cancel Then
MsgBox("User clicked Cancel button")
End If
End Sub
End Class

Public Class Form2
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'The Windows Form Designer requires this call.
InitializeComponent()

'Add any initialization after the InitializeComponent() call.

End Sub

'Form overrides Dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Friend WithEvents btnOK As System.Windows.Forms.Button
Friend WithEvents btnCancel As System.Windows.Forms.Button

'The Windows Form Designer requires this code.
Private components As System.ComponentModel.Container

'NOTE: The Windows Form Designer requires this procedure.
'You can use the Windows Form Designer to modify the code.
'However, do not use the Code editor to modify it.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnOK = New System.Windows.Forms.Button()
Me.btnCancel = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'btnOK
'
Me.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK
Me.btnOK.Location = New System.Drawing.Point(80, 72)
Me.btnOK.Name = "btnOK"
Me.btnOK.TabIndex = 0
Me.btnOK.Text = "OK"
'
'btnCancel
'
Me.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.btnCancel.Location = New System.Drawing.Point(160, 72)
Me.btnCancel.Name = "btnCancel"
Me.btnCancel.TabIndex = 1
Me.btnCancel.Text = "Cancel"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(320, 101)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnCancel, Me.btnOK})
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)

End Sub

#End Region

End Class

Verify That It Works

  1. Press the CTRL+F5 key combination to run the project. Form1 appears on the screen.
  2. Click Show Form2.
  3. In the resultant dialog box, click either OK or Cancel to close the form. A message box displays which button you clicked.

    If you clicked OK, the message box displays the following caption:
    User clicked OK button
    If you clicked Cancel, the message box displays the following caption:
    User clicked Cancel button
Propiedades

Id. de artículo: 315711 - Última revisión: 11 may. 2007 - Revisión: 1

Comentarios