Microsoft KB Archive/315711

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

Article ID: 315711

Article Last Modified on 5/11/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q315711



IN THIS TASK
SUMMARY
 * Requirements
 * Using the DialogResult Property
 * Complete Code Listing
 * Verify That It Works



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.

back to the top

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

back to the top

Using the DialogResult Property
This sample uses the DialogResult property to determine which button a user clicks to close a form.  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:

http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx

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

http://msdn2.microsoft.com/en-us/library/ms171843.aspx

 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 . 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. Click Open. A new form named Form2 is created. 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</li> Text: OK</li></ul>

Button2

<ul> Name: btnCancel</li> DialogResult: Cancel</li> Text: Cancel</li></ul>

</li> 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.</li>  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 </li>  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(&quot;User clicked OK button&quot;) ElseIf dr = DialogResult.Cancel Then MsgBox(&quot;User clicked Cancel button&quot;) End If                   </li></ol>

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.

back to the top

Complete Code Listing
Public Class Form1 Inherits System.Windows.Forms.Form


 * 1) Region &quot; Windows Form Designer generated code &quot;

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 = &quot;Button1&quot; Me.btnShowForm2.TabIndex = 0 Me.btnShowForm2.Text = &quot;Show Form2&quot; '   '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 = &quot;Form1&quot; Me.Text = &quot;Form1&quot; Me.ResumeLayout(False)

End Sub


 * 1) 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(&quot;User clicked OK button&quot;) ElseIf dr = DialogResult.Cancel Then MsgBox(&quot;User clicked Cancel button&quot;) End If End Sub End Class

Public Class Form2 Inherits System.Windows.Forms.Form


 * 1) Region &quot; Windows Form Designer generated code &quot;

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 = &quot;btnOK&quot; Me.btnOK.TabIndex = 0 Me.btnOK.Text = &quot;OK&quot; '   'btnCancel '   Me.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel Me.btnCancel.Location = New System.Drawing.Point(160, 72) Me.btnCancel.Name = &quot;btnCancel&quot; Me.btnCancel.TabIndex = 1 Me.btnCancel.Text = &quot;Cancel&quot; '   '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 = &quot;Form2&quot; Me.Text = &quot;Form2&quot; Me.ResumeLayout(False)

End Sub


 * 1) End Region

End Class back to the top

Verify That It Works
<ol> Press the CTRL+F5 key combination to run the project. Form1 appears on the screen.</li> Click Show Form2.</li> 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

</li></ol>

back to the top

Keywords: kbvs2005swept kbvs2005applies kbhowtomaster KB315711

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.