9 Replies - 928 Views - Last Post: 20 August 2013 - 08:26 AM Rate Topic: -----

#1 twins2me  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 30-July 13

Trouble getting selected file location to populate text box

Posted 14 August 2013 - 07:39 PM

I'm having trouble getting a selected file from an open file dialog result to populate a text box with the file's location.

 Private Sub OpenFileButton_Click(sender As System.Object, e As System.EventArgs) Handles OpenFileButton.Click

        'Open the file reader
        Dim ResponseDialogResult As DialogResult

        'Set up and display the open file dialog
        With DataOpenFileDialog
            'Begin in the current folder
            .InitialDirectory = Directory.GetCurrentDirectory()
            .Title = "Select File or Directory for File"
            'Display the Open File Dialog box
            If ResponseDialogResult = .ShowDialog(Me) <> DialogResult.Cancel Then
                FileLocationTextBox.Text = Windows.Forms.DialogResult.OK
            End If
        End With


    End Sub



It's populating just the number "1" in the box. How do I fix this?

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble getting selected file location to populate text box

#2 ybadragon  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 178
  • View blog
  • Posts: 1,106
  • Joined: 11-May 12

Re: Trouble getting selected file location to populate text box

Posted 14 August 2013 - 07:45 PM

OpenFileDialog.FileName is what you want to find. You basiacally want to store the selected file in a string and then assign that string to the textboxes .text property
Was This Post Helpful? 1
  • +
  • -

#3 twins2me  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 30-July 13

Re: Trouble getting selected file location to populate text box

Posted 14 August 2013 - 08:04 PM

Thank you! I will check it out and let you know how it works.


After reading it, how do I not use a rich text box? I just want to use a simple text box.

This post has been edited by twins2me: 14 August 2013 - 08:13 PM

Was This Post Helpful? 0
  • +
  • -

#4 twins2me  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 30-July 13

Re: Trouble getting selected file location to populate text box

Posted 14 August 2013 - 08:35 PM

I figured it out.

textbox1.text = openfiledialog1.filename



Now how do I get my application to read the file from that location? This is what I put, but it says that the file doesn't exist.

If File.Exists(FileLocationTextBox.Text = DataOpenFileDialog.FileName) Then
   inlines = File.ReadAllText(FileLocationTextBox.Text = DataOpenFileDialog.FileName).Split(CChar(vbNewLine))
            Else
                MessageBox.Show("File not found in " & Application.StartupPath)
            End If


This post has been edited by twins2me: 14 August 2013 - 08:42 PM

Was This Post Helpful? 0
  • +
  • -

#5 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Trouble getting selected file location to populate text box

Posted 15 August 2013 - 01:01 AM

You really should turn Option Strict On. It really does help. For example, with Option Strict On, your line:

If File.Exists(FileLocationTextBox.Text = DataOpenFileDialog.FileName) Then


shows a squiggly blue underline under the entire contents of the part in parentheses. Driving your cursor over the squiggly underline shows an ifo box that says [b"]Option Strict On disallows implicit conversions from 'Boolean' to 'String'"[/b]. Now, you may wonder what that means, and how to go about figuring it out, but at least it shows you that the line is in error.

An "=" sign within an If statement signifies that the compiler should check to see if the FileLocationTextBox.Text = DataOpenFileDialog.FileName condition is tTrue or False

As well, the ReadAllText line is going to fail for a similar reason.

Have a lok at OpenFileDialog in the docs, or on the MSDN site. Have a look at how you go about using it...

Basically, you ShowDialog, get the return value, then grab the filename from it if a selection was made.

This post has been edited by lar3ry: 15 August 2013 - 01:03 AM

Was This Post Helpful? 1
  • +
  • -

#6 twins2me  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 30-July 13

Re: Trouble getting selected file location to populate text box

Posted 19 August 2013 - 06:21 PM

I still can get this part figured out. I have searched other forums and the MSDN site, but haven't found anything like what I'm trying to do. I need my application to open the file from the file location that is populated in a text box.
Was This Post Helpful? 0
  • +
  • -

#7 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Trouble getting selected file location to populate text box

Posted 19 August 2013 - 06:44 PM

View Posttwins2me, on 19 August 2013 - 07:21 PM, said:

I still can get this part figured out. I have searched other forums and the MSDN site, but haven't found anything like what I'm trying to do. I need my application to open the file from the file location that is populated in a text box.

A string that's in a TextBox is no different than a string in a variable, or even a literal string. Once you have the string, you just need to use it in whatever method you want, to load the file. For example;

        Dim sr As New StreamReader("J:\Foo\blap.txt")
        Dim fileContents = sr.ReadToEnd
        sr.Dispose()

        'is the same as:

        Dim sr As New StreamReader(TextBox1.Text)
        Dim fileContents = sr.ReadToEnd
        sr.Dispose()


Was This Post Helpful? 0
  • +
  • -

#8 twins2me  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 30-July 13

Re: Trouble getting selected file location to populate text box

Posted 19 August 2013 - 06:55 PM

I can't hard code the program file's location as I am going to have many files. I need to have it where the user can select their file to upload the data from. And have the application start the reading process from that file selected.
Was This Post Helpful? 0
  • +
  • -

#9 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Trouble getting selected file location to populate text box

Posted 19 August 2013 - 08:49 PM

View Posttwins2me, on 19 August 2013 - 07:55 PM, said:

I can't hard code the program file's location as I am going to have many files. I need to have it where the user can select their file to upload the data from. And have the application start the reading process from that file selected.

OK, well, you don't need a TextBox to hold the filename. You need to open a FileDialog, check the return value, then use the .FileName that the OpenFileDialog holds (provided the return value is good).

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim ofd As New OpenFileDialog
        If ofd.ShowDialog() = DialogResult.OK Then
            Dim sr As New StreamReader(ofd.FileName)
            ' read the file in however you want
            sr.Dispose()
        End If
    End Sub


Was This Post Helpful? 1
  • +
  • -

#10 twins2me  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 30-July 13

Re: Trouble getting selected file location to populate text box

Posted 20 August 2013 - 08:26 AM

As always, you're the best! It's working now. Thank you so very much! Programming with a head cold is not a good combination. :{
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1