Results 1 to 6 of 6

Thread: [02/03] OpenDialog

  1. #1

    Thread Starter
    Hyperactive Member FireKnox101's Avatar
    Join Date
    Aug 2005
    Location
    Snohomish,Washington
    Posts
    301

    Question [02/03] OpenDialog

    How do i use the open dialog to display text from a text file in a text box.

    i get this far then i dont know what to do.

    VB Code:
    1. Private Sub btntext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntext.Click
    2.         OpenFileDialog1.Filter = "Text Files (*.txt | *.txt"
    3.         If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

    Im currently using: VB.NET 2003, And VB 2005 Express
    My Projects
    Form Them Show Keypress In App
    Simple Ping Control

  2. #2
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    Re: [02/03] OpenDialog

    All the dialog does is give the user a way to get the path of the file. You have to actually open the file yourself. You would open the text file using a Streamreader using the path returned from the Dialog (OpenFileDialog1.Filename), then you can use the .ReadToEnd method in order to read the entire contents of the file and set that to your textbox text property. There are plenty examples on this forum on using streamreaders to read text files if you use the search function.

  3. #3

    Thread Starter
    Hyperactive Member FireKnox101's Avatar
    Join Date
    Aug 2005
    Location
    Snohomish,Washington
    Posts
    301

    Re: [02/03] OpenDialog

    Ok i found a thread that helped me here is what i did.

    VB Code:
    1. Private Sub btntext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntext.Click
    2.         Dim MyReader As New System.IO.StreamReader(OpenFileDialog1.FileName)
    3.         OpenFileDialog1.Filter = "Text Files (*.txt | *.txt"
    4.         If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
    5.             TextBox1.Text = MyReader.ReadToEnd()
    6.         End If

    But it halts at this line of code.

    Dim MyReader As New System.IO.StreamReader(OpenFileDialog1.FileName)

    did i mess up some where??

    Im currently using: VB.NET 2003, And VB 2005 Express
    My Projects
    Form Them Show Keypress In App
    Simple Ping Control

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,439

    Re: [02/03] OpenDialog

    There's no point opening the StreamReader until the user has selected a file. The FileName property won't contain the path to the selected file until AFTER ShowDialog has returned OK. You would open the StreamReader inside the If block.

  5. #5

    Thread Starter
    Hyperactive Member FireKnox101's Avatar
    Join Date
    Aug 2005
    Location
    Snohomish,Washington
    Posts
    301

    Re: [02/03] OpenDialog

    I got it now thank you both for your help

    Im currently using: VB.NET 2003, And VB 2005 Express
    My Projects
    Form Them Show Keypress In App
    Simple Ping Control

  6. #6
    Hyperactive Member francisstokes's Avatar
    Join Date
    May 2005
    Location
    Kent, England
    Posts
    272

    Re: [02/03] OpenDialog

    Your gonna want to correct this to:

    VB Code:
    1. OpenFileDialog1.Filter = "Text Files (*.txt | *.txt"   '< add the other bracket

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