CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2009
    Location
    Kuwait
    Posts
    170

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

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    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.

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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
    Last edited by DataMiser; August 26th, 2009 at 04:06 PM.

  4. #4
    Join Date
    Jul 2009
    Location
    Kuwait
    Posts
    170

    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
    Last edited by makdu; August 27th, 2009 at 04:55 AM.

  5. #5
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    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 (*.*)|*.*"

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: browse path copying to textbox

    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
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Jul 2009
    Location
    Kuwait
    Posts
    170

    Re: browse path copying to textbox

    thank you all.
    That worked with your suggestion

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured