Solved

How do I load the contents of a plain text file into a textbox or listbox?

Posted on 2006-10-20
4
216 Views
Last Modified: 2010-04-23
EE Experts:

I have been researching this question and I have not found an answer. I have found some interesting discussion about StreamReader and StreamWriter classes; but nothing that tells me how to get the data to/from a textbox or listbox to/from a StreamReader/StreamWriter object.

TedPalmer
0
Comment
Question by:Ted Palmer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17778760
The TextBox is the simpler case...

In VB.Net 2003 (or below):

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sr As New System.IO.StringReader("c:\someFile.txt")
        TextBox1.Text = sr.ReadToEnd
        sr.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim sw As New System.IO.StreamWriter("c:\someFile.txt")
        sw.Write(TextBox1.Text)
        sw.Close()
    End Sub

In VB.Net 2005:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = System.IO.File.ReadAllText("c:\someFile.txt")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        System.IO.File.WriteAllText("c:\someFile.txt", TextBox1.Text)
    End Sub
0
 

Author Comment

by:Ted Palmer
ID: 17780970
Idle_Mind:

In your VB.NET 2003 Sub Button1_Click example is that supposed to be "StringReader" or "StreamReader"? Is there a way that I can associate the input file name with the application object so that I don't have to hard code the full drive letter\path\filename in my program?

Thank you,
TedPalmer
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 17780985
You're right...that is supposed to be a StreamReader instead of a StringReader.  Sorry about that...

You can get the applications path/name from the Application class:

See:
System.Windows.Forms.Application.ExecutablePath
http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.executablepath.aspx
System.Windows.Forms.Application.StartupPath
http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.startuppath.aspx
0
 

Author Comment

by:Ted Palmer
ID: 17781032
Idle_Mind:

THANK YOU..!! You do good work..!!

TedPalmer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

735 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