Solved

OpenFileDialog??

Posted on 2007-11-16
9
315 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 70

Expert Comment

by:Éric Moreau
ID: 20299524
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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
 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
send messages to whatsapp programatically 2 48
VB.Net WebRequest Code from JSON curl 9 14
VB.Net - MemoryMappedFiles - Confirm receipt 2 19
vb.net class 3 16
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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