Solved

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

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now