filedialog?

LeonLanford

Active member
Joined
Dec 2, 2008
Messages
28
Programming Experience
Beginner
Hi..

How to browse file using file dialog? My file dialog has only folder to be browsed.. there's no file :(

the code is like this

VB.NET:
Public Class Form1

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        browse.ShowDialog()


    End Sub

End Class

thanks.. hope you can help me :D
 
Last edited by a moderator:
Are you talking about a FolderBrowser dialog or the OpenFileDialog? I have no idea what dialog your "browse" one is.
 
I was using the folder FolderBrowser, I just know that I can select file using OpenFileDialog, but now I don't know how to catch the path of the selected file..
I only know how to open dialog :(

VB.NET:
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        filedialog.ShowDialog()
    End Sub
 
Your posts are a little confusing so I'll explain both:
The FolderBrowser dialog allows the user to select a folder and it has nothing to do with files whatsoever.

The OpenFileDialog allows the user to select one or more files and are not allowed to select just a folder. If you want them to select a file then find out what folder that file resides in it's just a matter of:
VB.NET:
Dim TheFolder As String = MyOpenFileDialog.FileName.Replace(IO.Path.GetFileName(MyOpenFileDialog.FileName, String.Empty)
That will take the file's place replace just the file name (and extension) with an empty string which leaves you with the exact path of the folder the file resides in.
 
Really sorry.. my english is bad and I'm really newbie at vb.net..

I mean the OpenFileDialog one, at the first post I didn't know OpenFileDialog exists

I just tried the code and got error message
"Too many arguments to 'Public Share Function GetFilename(path as string) as string'"

I tried to modify to
VB.NET:
Dim TheFolder As String = filedialog.FileName.Replace(IO.Path.GetFileName(filedialog.FileName))
but it says overload replace failed..
 
Try

VB.NET:
Dim TheFolder As String = IO.Path.GetDirectoryName(Me.OpenFileDialog1.FileName)

Thanks it's worked :D :D

last.. how to open a lot of files?
how to handle each string? array?

The OpenFileDialog allows the user to select one or more files and are not allowed to select just a folder. If you want them to select a file then find out what folder that file resides in it's just a matter of:
Code:
 
Change Multiselect to True in the OpenFileDialog's properties or from code

VB.NET:
Me.OpenFileDialog1.Multiselect = True

If you take a look at Me.OpenFileDialog1.FileNames you'll see "Public ReadOnly Property FileNames() As String()".

Knowing this you can access the array in several ways.

VB.NET:
		For Each file As String In Me.OpenFileDialog1.FileNames
			MessageBox.Show(file)
		Next

VB.NET:
		For i As Integer = 0 To Me.OpenFileDialog1.FileNames.Length - 1
			MessageBox.Show(Me.OpenFileDialog1.FileNames(i).ToString())
		Next
 
Thanks.. you really help me a lot.. :D :D

and I just found out it can be accessed without tostring also :D
Me.filedialog.FileNames(i)
 
Hi.. I'm having problem with open file dialog

When I first open the program then select(browse) the files, if I press the cancel button, the entry files are not selected(not go to the data grid), but after the first try, if I cancel, the entry from the first try got inserted, Is this bug? I've used filedialog.dispose().. :(
I've tried to put the dispose after showdialog() but it still doesn't work..

here's the code

VB.NET:
        FileDialog.ShowDialog()

        total = FileDialog.FileNames.Length
        uplot = New String(total) {}
        If total > 0 Then
            dgv_uplot.Rows.Add(total)
        End If

        FileDialog.Dispose()

        For i = 0 To total - 1
            uplot(i) = Me.filedialog.FileNames(i).ToString

            dgv_uplot.Item(1, baris).Value = uplot(i)
            baris += 1
        Next

        FileDialog.Dispose()
 
Last edited:
ShowDialog is a function, so you can read the result from the return value, if was Ok, Cancel etc.
 
Back
Top