Moonbiter

I need help in figuring out how to browse for a file, then have it return it's path as a string to put in a text box. I figured out how to browse for a folder and do that same thing, but I need to be able to pick a file.

Right now I have a button to activate the browsedialog, but I only see a browsefolder or an openfile for dialogs. I don't need to open the file, on to get it's path and then have it populate a textbox.

Anyone have some hints on how to go about this

Thanks.


Re: Visual Basic General Need help with browsing for files

Aw Ali

If you don't want to open the file, the best way to search and return the path of the file. You can accomplish this using 'My' namespace.

Dim myfile As String

For Each myfile In My.Computer.FileSystem.GetFiles(location, FileIO.SearchOption.SearchAllSubDirectories, "*.doc")

'location=parent folder to search

'.doc=extension of the file you are searching for.

' If you don't specify this, it will search all files in the specified directory.

'----------------------------------------------------------------------

'You can inspect the file name here. Just create a file from myfile string and process it

MsgBox(myfile)

Next

You can also use OpenFileDialog, but you have to click on the file and click Open (Won't open it) and use its fileName to get the filename.






Re: Visual Basic General Need help with browsing for files

JohnWein

Use the OpenFileDialog Control. It's in the Toolbox.



Re: Visual Basic General Need help with browsing for files

Moonbiter

How do I get the path to display in the textbox after using openfiledialog

textbox1.text =

I tried reading up on the Openfiledialog last night but it was pretty confusing to me. I'm new to visual basic programming so that doesn't help either. Maybe I'm just tired right now, because I fought with my search code for almost 2 weeks, but finally got it working 2 days ago. I'm sure there is a better way of doing it, but I am happy with my results. Now I've just hit another snag and I think my mind is too exhausted and I'm missing something easy.




Re: Visual Basic General Need help with browsing for files

Aw Ali

Use this code. You probably done some of it already, but I am giving the whole picture.

Dim fileBrowser As New OpenFileDialog

If fileBrowser.ShowDialog() = Windows.Forms.DialogResult.OK Then

TextBox1.text=folder.FileName

End If






Re: Visual Basic General Need help with browsing for files

JohnWein

You'll probably use the OpenFileDialog Control a lot. You have to parse the returned FileName yourself.

Private Sub Button1_Click_1(ByVal sender As System.Object, _

ByVal e As System.EventArgs) _

Handles Button1.Click

If OpenFileDialog1.ShowDialog() = _

Windows.Forms.DialogResult.OK Then

Dim I As Integer = InStrRev(OpenFileDialog1.FileName, "\")

PathLabel.Text = Strings.Left(OpenFileDialog1.FileName, I - 1)

FileLabel.Text = Strings.Mid(OpenFileDialog1.FileName, I + 1)

End If

End Sub





Re: Visual Basic General Need help with browsing for files

Moonbiter

Well, I didn't use your suggestion or the other, but you did get my creative mind flowing again and I came up with this bit of code that does exactly what I want. Let me know if this is a hack job though, since I don't want to develope bad habits.

If
OpenFileDialog1.ShowDialog() = DialogResult.OK Then
ImageTextBox.Text = OpenFileDialog1.FileName
End If




Re: Visual Basic General Need help with browsing for files

Anonymous

Dim Result as DialogResult

Dim OpenFile as new OpenFileDialog

OpenFile.Filter = "Text | *txt"

OpenFile.Filename = " "

OpenFile.Title = "Open File"

Result = OpenFile.showdialog()

If Result <> Cancel And OpenFile.Filename <> "" Then

TextBox.text = OpenFile.filename

End If

I think this code should do it. You code probably works too but it never hurts to know more than one method. The only difference is the conditions I use.