Windows Phone Developers

Saturday, June 23, 2007

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.


For CommonDialog implementation in VB6.0 refer http://vbadud.blogspot.com/2007/06/visual-basic-common-dialog.html

Upgrade Notes
When a Visual Basic 6.0 application is upgraded to Visual Basic 2005, any existing DirListBox controls are upgraded to the VB6.DirListBox control that is provided as a part of the compatibility library (Microsoft.VisualBasic.Compatibility). Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

11 comments:

  1. I have been searching for what seems like ages, and google now decides to help me for once. I'm somewhat of a noob at this, so this post was really usefull.

    ReplyDelete
  2. Thanks a bunch for this post, its been a big help to me. I would like to know, though, how to get the name of the file to show up in that text box next to the button. I copied the code exactly, and it does allow me to select the file, but then it doesnt show up in the text box. What am I missing?

    ReplyDelete
  3. robo,
    You will need to add code to place the name of the file in you text box.

    Example:
    after the following line in the code
    sMyFile = MyFileOpen.FileName

    add the following (using your textbox's name of course)
    me.txtMyTextBox.Text = sMyFile

    ReplyDelete
  4. Hi,
    I have written a web application, which creates word doc and saves it. But I want to capture Close event of word doc from webapp, so that I can do further processing. Can you please help? Is there any component in web forms which is equivalent to Process in winform?
    Thank you,
    Sandhy.

    ReplyDelete
  5. Hi,

    hope this sample will help more
    http://developerskb.blogspot.com/2008/07/how-to-use-openfiledialog-for-uploading.html

    ReplyDelete
  6. I am looking for what Robo asked for, but when I put this: me.txtMyTextBox.Text = sMyFile in, it says Name 'sMyFile' is not declared. How do I make this work?

    ReplyDelete
  7. You can declare the variable and continue

    Dim sMyFile as string

    sMyFile = MyFileOpen.FileName

    me.txtMyTextBox.Text = sMyFile

    ReplyDelete
  8. I want my richtextbox to show the text of the file i open up. not the location of the file i selected. How do i do that?

    ReplyDelete
  9. You might actually want to use the OpenFileDiag1 you created in the designer when popping op the dialog. In the current sample, a new dialog is created for every clock as far as i can see.

    ReplyDelete
  10. thanks for the code

    ReplyDelete

kbAlertz.com :: Visual Studio 2005

kbAlertz.com :: Visual Studio 2008

kbAlertz.com :: Visual Basic 2005