[RESOLVED] browse path copying to textbox
Hi all,
I am making an application. Here one of the field is a picture. I need to browse to this picture folder from vb.net application and then save the path to a foxpro table. Something similar to what we have when a setup is run, we use to browse to target location and the path value will be there in the text box.
What is the approach to be tried out.
Re: browse path copying to textbox
You can use the OpenFileDialog for browsing to the file location and this dialog has a property called FileName that will tell you which file was selected along with the path.
Re: browse path copying to textbox
If you are looking for a file then the method above would be correct. If you are just wanting to browse for a folder then you would want to use the folderbrowsedialog instead.
Code:
Public Function GetFolder(Optional ByVal userPrompt As String = "Select Folder", Optional ByVal StartFolder As String = "c:\") As String
' First create a FolderBrowserDialog object
Dim FolderBrowserDialog1 As New FolderBrowserDialog
Dim ReturnString As String = ""
' Then use the following code to create the Dialog window
' Change the .SelectedPath property to the default location
With FolderBrowserDialog1
' Desktop is the root folder in the dialog.
.RootFolder = Environment.SpecialFolder.Desktop
' Select the start folder given as starting location.
.SelectedPath = StartFolder
' Prompt the user with a custom message.
.Description = userPrompt
If .ShowDialog = DialogResult.OK Then
' Return the selected folder if the user clicked on the OK button.
ReturnString = .SelectedPath
End If
End With
FolderBrowserDialog1 = Nothing
Return ReturnString
End Function
Re: browse path copying to textbox
thanks for the help. I was looking for the openfiledialog only.
While setting the filter here, i am facing a problem. I want to select only image files. So i wrote like this
Code:
openFileDialog1.Filter = "image (*.jpg) |*.bmp;*.gif|All files (*.*)|*.*"
but this is not showing the files with jpg extension, and i have to select All files option to view that. Where am i doing it wrong?
this is the complete code
Code:
Dim openFileDialog1 As System.Windows.Forms.OpenFileDialog
openFileDialog1 = New System.Windows.Forms.OpenFileDialog()
openFileDialog1.InitialDirectory = "D:\"
' openFileDialog1.Filter = "image (*.jpg) |*.bmp;*.rtf|(*.txt) |*.txt|(*.*) |*.*"
'openFileDialog1.Filter = " (*.jpg) |*.bmp;*.rtf|(*.txt) |*.txt|(*.*) |*.*"
openFileDialog1.Filter = "*.jpg|*.jpg |All files (*.*)|*.*"
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
locpic.Text = openFileDialog1.FileName
End If
Re: browse path copying to textbox
Your Filter property should ideally look like this
Code:
"JPG files (*.jpg)|*.jpg|Text files (*.txt)|*.txt|All files (*.*)|*.*"
Re: browse path copying to textbox
Quote:
From MSDN:
Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*
Code:
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myStream As Stream = Nothing
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
myStream = openFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
' Insert code to read the stream here.
End If
Catch Ex As Exception
MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
Finally
' Check this again, since we need to make sure we didn't throw an exception on open.
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End Try
End If
End Sub
Re: browse path copying to textbox
thank you all.
That worked with your suggestion