' OpenFileDialog Project ' Show how to use an OpenFileDialog and how to cancel a ' form Closing event. Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. 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 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents mnuExit As System.Windows.Forms.MenuItem Friend WithEvents mnuShowFileDialog As System.Windows.Forms.MenuItem Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents txtFileName As System.Windows.Forms.TextBox Private Sub InitializeComponent() Me.MainMenu1 = New System.Windows.Forms.MainMenu Me.mnuExit = New System.Windows.Forms.MenuItem Me.mnuShowFileDialog = New System.Windows.Forms.MenuItem Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog Me.txtFileName = New System.Windows.Forms.TextBox Me.Label1 = New System.Windows.Forms.Label Me.SuspendLayout() ' 'MainMenu1 ' Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuExit, Me.mnuShowFileDialog}) ' 'mnuExit ' Me.mnuExit.Index = 0 Me.mnuExit.Text = "Exit" ' 'mnuShowFileDialog ' Me.mnuShowFileDialog.Index = 1 Me.mnuShowFileDialog.Text = "ShowFileDialog" ' 'txtFileName ' Me.txtFileName.Location = New System.Drawing.Point(24, 16) Me.txtFileName.Name = "txtFileName" Me.txtFileName.Size = New System.Drawing.Size(576, 20) Me.txtFileName.TabIndex = 0 Me.txtFileName.Text = "" ' 'Label1 ' Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(240, 48) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(160, 24) Me.Label1.TabIndex = 1 Me.Label1.Text = "Selected File Name" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(632, 89) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.txtFileName) Me.Menu = Me.MainMenu1 Me.Name = "Form1" Me.Text = "OpenFileDialog Example" Me.ResumeLayout(False) End Sub #End Region Private Sub mnuExit_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles mnuExit.Click Me.Close() End Sub Private Sub mnuShowFileDialog_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles mnuShowFileDialog.Click If OpenFileDialog1.ShowDialog() = DialogResult.OK Then txtFileName.Text = OpenFileDialog1.FileName Else txtFileName.Text = "Cancel Selected" End If End Sub Private Sub Form1_Closing(ByVal sender As Object, _ ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing If MessageBox.Show("Do you really want to close?", "", _ MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No Then e.Cancel = True End If End Sub End Class