Link to home
Start Free TrialLog in
Avatar of Lee Faulkner
Lee FaulknerFlag for United Kingdom of Great Britain and Northern Ireland

asked on

OpenFileDialog??

Hi Guys,

How can I use the openfiledialog to get the string of a filename i.e. C:\Folder\file.ext and put this string into a text box?

I think it is openfiledialog I have to use.

Lee
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Try this:
Dim openfile As New OpenFileDialog
Dim myfilename As String = Nothing
 
openfile.CheckFileExists = True
openfile.CheckPathExists = True
openfile.ShowDialog()
 
If openfile.FileName <> "" Then
   myfilename = openfile.FileName
End If

Open in new window

In the above example the myfilename will get the full path of the selected file. If you only need the file name (without path) you can do
Dim openfile As New OpenFileDialog
Dim myfilename As String = Nothing
 
openfile.CheckFileExists = True
openfile.CheckPathExists = True
openfile.ShowDialog()
 
If openfile.FileName <> "" Then
   Dim file As New System.Io.FileInfo(openfile.FileName)
   myfilename = file.Name
   Debug.Print myfilename.ToString()
End If

Open in new window

Avatar of Lee Faulkner

ASKER

Ok,

So if that opens the openfiledialog and prints the full path and file name then how does the script determine which text box to print the information to? I have 35 text boxes on one form and underneath each text box if the button to change the location of the file.

So in theory I need the open location button underneath text box 1 to open the openfiledialog and then when the user selects the file they want to open in the openfiledialog it prints the location into text box 1. I also want the openfiledialog to only show audio files.

Lee
>> So in theory I need the open location button underneath text box 1 to open the openfiledialog and then when the user selects the file they want to open in the openfiledialog it prints the location into text box 1.
Yes. If you only want to use one button you can do a script that detects the empty textboxes and write there. You can also change the textbox's for a listview or listbox.

>> I also want the openfiledialog to only show audio files
Add this before the showdialog method and replacing by the files that you want.
openfile.Filter = "MP3 Files (*.mp3)|*.mp3|Other Audio Files|*.*"

Ok cool I have the filters in place.

So how do I setup the script so that open location button 1 opens a file dialog that prints the information into text box1. and open location button 2 links to text box 2 etc?

Lee
You can do this:

' Put your code in a sub

Private Sub GetFile(ByVal tb As TextBox)
Dim openfile As New OpenFileDialog

openfile.CheckFileExists = True
openfile.CheckPathExists = True
openfile.Filter = "MP3 Files (*.mp3)|*.mp3|Other Audio Files|*.*"
openfile.ShowDialog()
 
If openfile.FileName <> "" Then
  tb.Text = openfile.FileName.ToString()
Else
  tb.Text = ""
End If

Then on click event for each button you do
  Call GetFile(TextBox1) ' for TextBox1
  Call GetFile(TextBox2) ' for TextBox2
  ...
ASKER CERTIFIED SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
cool that all sorted now! thanks very much ipaulino!