Solved

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

Posted on 2006-10-20
4
220 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 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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