?
Solved

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

Posted on 2006-10-20
4
Medium Priority
?
231 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
  • 2
  • 2
4 Comments
 
LVL 86

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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

839 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