Visual Studio Development Bookmark and Share   
 index > Visual Basic Express Edition > Getting errors with OpenFileDialog
 

Getting errors with OpenFileDialog

So I have a program that when a person clicks a button, the following happens...

        Dim openfile As New OpenFileDialog
        openfile.Multiselect = True
        openfile.Title = "Add Music Files"
        openfile.Filter = "wav files |*.wav"
        openfile.FileName = ""
        openfile.ShowDialog()
        file = System.IO.Path.GetFullPath(openfile.FileName)
        filename = System.IO.Path.GetFileName(openfile.FileName)
        txtPath.Text = filename

This is all fine. however, when the user clicks Cancel in this section, a runtime error happens because System.IO.Path.GetFullPath(openfile.FileName) = "". What code to I need to put in to stop this from happening?

I know it's preaty simple, I just want to know...
joshlama  Saturday, October 17, 2009 12:32 PM
If openfile.ShowDialog = DialogResult.OK then ...
  • Marked As Answer byjoshlama Sunday, October 18, 2009 7:23 AM
  •  
JohnWein  Saturday, October 17, 2009 1:52 PM
If openfile.ShowDialog = DialogResult.OK then ...
  • Marked As Answer byjoshlama Sunday, October 18, 2009 7:23 AM
  •  
JohnWein  Saturday, October 17, 2009 1:52 PM
i tried this:

Dim openfile As New OpenFileDialog
        openfile.Multiselect = True
        openfile.Title = "Add Music Files"
        openfile.Filter = "wav files |*.wav"
        openfile.FileName = ""
        openfile.ShowDialog()
        If openfile.FileName <> "" Then
            file = System.IO.Path.GetFullPath(openfile.FileName)
            filename = System.IO.Path.GetFileName(openfile.FileName)
            txtPath.Text = filename
        End If
hope this helps
NegativeX69  Saturday, October 17, 2009 1:52 PM
That is wrong . What if the user chooses a file but then clicks cancel ? Your code would run even though it shouldn't because the user pressed cancel . Do as John said after you show the dialog .
coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
Please format the code in your posts with the button . Makes it easier to read .
bdbodger  Saturday, October 17, 2009 8:09 PM
wow, at least you were polite about me being wrong....nice manners
NegativeX69  Saturday, October 17, 2009 11:52 PM
Well not totally wrong just needed that other line to ensure that the user has pressed ok then you can check the filename . One thing though is the MultiSelect option . This code will work for a multiline textbox and the user selecting multiple files .

 
Dim openfile As New OpenFileDialog
        openfile.Multiselect = True
        openfile.Title = "Add Music Files"
        openfile.Filter = "wav files |*.wav"
        If openfile.ShowDialog() = Windows.Forms.DialogResult.OK Then
            If openfile.FileNames.Count <> 0 Then
                Dim files() As String = openfile.FileNames
                For Each FN As String In files
                    txtPath.AppendText(FN & vbCrLf)
                Next
            End If
        End If

coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
Please format the code in your posts with the button . Makes it easier to read .
bdbodger  Sunday, October 18, 2009 4:56 AM

You can use google to search for other answers

Custom Search

More Threads

• MsComm1 "Framing Error"
• Email Code Help...
• Property 'Kind' is 'ReadOnly'
• Stop Website Redirecting in VB 2005 Express
• Create a Rad software or block programming software like lego
• How do i print image and text in one single printing action...
• Simple game wont run...
• [2005] NotifyIcon's BalloonToolTip
• Confused about two versions of my data in my project and my ability to save data
• Problem with Db not saving--Solution is here!