Hey everyone! I'm having trouble with the Open File Dialog in VB.NET. It works fine because I am only returning the file path of the file selected, but for some reason the dialog box reloads every time it is clicked for the first time. For instance, the dialog opens and shows all files located under My Documents. It should only show .xml files though. After I select a file, it then reloads only the .xml files, which can then be selected.

I would obviously like the file to be selected after the user clicks the first time- not the second, so if anyone can help that would be great!!!!

I know this probably sounds really confusing- sorry!

Private Sub OpenItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenItem.Click
        Dim _clickFile As Integer = OpenFile.ShowDialog()
        Dim StartOpen As New StartTroubleshooting

        Try
            OpenFile.Filter = "XML Files(*.xml)|*.xml"  'Only show XML files in Open Dialog Window
            OpenFile.InitialDirectory = "/My Documents"
            OpenFile.ShowDialog()
            If _clickFile = Windows.Forms.DialogResult.OK Then  ' If Open File Dialog is opened
                If _clickFile = DialogResult.Cancel Then    ' If cancel button is hit, go back to screen
                    Me.Show()
                Else    
                    _filePath = Path.GetFullPath(OpenFile.FileName) ' get file path of file selected
                    StartOpen.Show()
                    OpenFile.Dispose()
                    Me.Hide()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

Thanks for any help!!!!

Recommended Answers

All 2 Replies

I read your question more and more and I didn't know what you need or what your problem is!
But if you need to show selected file's path

Dim openFile As New OpenFileDialog()
        openFile.Filter = "XML Files(*.xml)|*.xml"  'Only show XML files in Open Dialog Window
        openFile.InitialDirectory = "/My Documents"
        If openFile.ShowDialog() = DialogResult.OK Then
            MessageBox.Show(openFile.FileName)
        End If

Thank you so much, that worked! Even if you didn't completely understand the question, which I understand haha : )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.