?
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
?
226 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

752 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