Solved

OpenFileDialog??

Posted on 2007-11-16
9
314 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:lfaulkneruk
  • 5
  • 3
9 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 20299359
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

0
 
LVL 48

Expert Comment

by:jpaulino
ID: 20299429
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

0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 20299524
0
 

Author Comment

by:lfaulkneruk
ID: 20299642
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 48

Expert Comment

by:jpaulino
ID: 20299706
>> 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|*.*"

0
 

Author Comment

by:lfaulkneruk
ID: 20299742
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
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 20299911
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
  ...
0
 
LVL 48

Accepted Solution

by:
jpaulino earned 250 total points
ID: 20299914
Is missing a End 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
End Sub ' <----- missing in my last post
0
 

Author Comment

by:lfaulkneruk
ID: 20300089
cool that all sorted now! thanks very much ipaulino!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with error message for ReportViewer in VS2015 4 44
help with issues with ReportViewer in VS2015 5 38
Saveas need to save a copy 16 26
vb.net 2 37
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now