C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Then: In the Button1_Click event handler, add a called to OpenFileDialog1.ShowDialog.
Tip: Assign a DialogResult Dim to the result of the ShowDialog Function. Then test the DialogResult with an If-Statement.
VB.NET program that uses OpenFileDialog, ShowDialog
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Call ShowDialog.
Dim result As DialogResult = OpenFileDialog1.ShowDialog()
' Test result.
If result = Windows.Forms.DialogResult.OK Then
' Do something.
End If
End Sub
End Class
And: When you close the OpenFileDialog, control flow returns to the Button1_Click event handler.
Note: When the file is read, and the OK button is pressed, the length of the file is displayed in the title bar of the window.
VB.NET program that uses OpenFileDialog, reads file
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Call ShowDialog.
Dim result As DialogResult = OpenFileDialog1.ShowDialog()
' Test result.
If result = Windows.Forms.DialogResult.OK Then
' Get the file name.
Dim path As String = OpenFileDialog1.FileName
Try
' Read in text.
Dim text As String = File.ReadAllText(path)
' For debugging.
Me.Text = text.Length.ToString
Catch ex As Exception
' Report an error.
Me.Text = "Error"
End Try
End If
End Sub
End Class