Friday 18 November 2011

OpenFileDialog in Visual Basic .Net

List Files in Visual Basic .Net / Visual Basic 2005

The Visual Basic 6.0 DirListBox control has been rendered obsolete by the OpenFileDialog and SaveFileDialog components in Visual Basic 2005.

Conceptual Differences
The Visual Basic 6.0 DirListBox control was typically used to display directories and paths in a File Open or Save dialog box.

In Visual Basic 2005, the Windows Forms OpenFileDialog and SaveFileDialog components provide the ability to create standard Windows dialog boxes for working with files, in most cases eliminating the need for the DirListBox control.


For this example, Let us have a form with a TextBox and a command button. When the command buton is pressed, the folderdialog is shown and then the selected folder is displayed in the textbox

Sample Form:




Add the FolderBrowser Dialog to the form from the Dialogs Collection (see below).




This control will not be placed on the form but on a separate tray at the bottom of the Windows Forms Designer. (see below)




Now in the click event for the Button have the following code:


Private Sub BtnFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFile.Click

Dim MyFileOpen As New System.Windows.Forms.OpenFileDialog
Dim bExOccured As Boolean
Dim retVal As DialogResult
Dim sMyFile As String
Try
' does not add an extension to a file name if the user omits the extension
MyFileOpen.AddExtension = True

'dialog box does not allow multiple files to be selected
MyFileOpen.Multiselect = False

MyFileOpen.Filter = "ASCII files (*.txt;*.log)|*.txt;*.log"

retVal = MyFileOpen.ShowDialog()
If retVal = Windows.Forms.DialogResult.OK Then

If MyFileOpen.CheckFileExists = True And MyFileOpen.CheckPathExists = True Then

sMyFile = MyFileOpen.FileName

End If
End If
Catch ex1 As AccessViolationException
MsgBox(ex1.StackTrace.ToString)
bExOccured = True
Catch ex As Exception
MsgBox(ex.StackTrace.ToString)
bExOccured = True
Finally
If bExOccured = True Then
MsgBox("Program executed with some errors!!!")
End If
End Try
End Sub

You can use filters to restrict the type of files that can be opened. A sample of common filters is given below

'MyFileOpen.Filter = "Microsoft Word Documents (*.doc)|*.doc|Microsoft Word Documents (*.rtf)|*.rtf"
'MyFileOpen.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF"
'MyFileOpen.Filter = "Microsoft Word Documents (*.doc;*.rtf)|*.doc;*.rtf"

'MyFileOpen.Filter = "Microsoft Excel Workbooks (*.xls)|*.xls"
'MyFileOpen.Filter = "Microsoft Excel Addins (*.xla;*.xll)|*.xla;*.xll"
'MyFileOpen.Filter = "All files (*.*)|*.*"
'MyFileOpen.Filter = "Text files (*.txt)|*.txt"


This class allows you to check whether a file exists and to open it. The ShowReadOnly property determines whether a read-only check box appears in the dialog box. The ReadOnlyChecked property indicates whether the read-only check box is checked.

Most of the functionality for this class is found in the FileDialog class.

Microsoft recommends that you use the OpenFileDialog and SaveFileDialog components to provide a consistent and familiar user experience. If you find it necessary to create your own file dialog boxes, Visual Basic 2005 does provide a DirListBox control as part of the Microsoft Visual Basic Compatibility Runtime library.

No comments:

Post a Comment