Page 1 of 1

[FIXED][StreamWriting/Reading] String problems

Posted: Fri Jul 03, 2009 4:49 am
by Click16
Ok, I am working with a StreamWriter. For the sake of demonstration, I will create a new app just for stream reading and writing.

Also, I'm using VB. This is because I started a project a while ago. I didn't know C# at the time, so I am continuing the project using VB. (I'm sure I can figure it out if C# is used though.)

This is the app I made for this:
Image


PROBLEM:
I will add this code to the button1.click event. (Button1 = Write)

Code: Select all

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Setting the File up
        If My.Computer.FileSystem.FileExists("C:\WriteFile.txt") = False Then
            MessageBox.Show("You Dumbass, make a text file called ''WriteFile'' in your C:\ Directory! >_<")
        Else

        End If

        ' Open a file for writing  
        Dim FILENAME As String = ("C:\WriteFile.txt")

        ' Create a new StreamWriter as nsr 
        Dim nsr As StreamWriter
        nsr = File.AppendText(FILENAME)
        'Write the contents of textbox1 to a new line.  
        nsr.WriteLine(TextBox1.Text)

        ' Close the stream  
        nsr.Close()

    End Sub
So I write the following lines of text into the text file using my new app.

Image

It writes!

Now lets get it to read.

I put this code into the button2.click event:

Code: Select all

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Creating a StreamRead
        Dim nsr As New StreamReader("C:\WriteFile.txt")

        'Simplifying the text
        Dim txt As String = nsr.ReadToEnd()
        'Adding the text to textbox2
        TextBox2.text = txt
        
        'Closing the Stream Reader
        nsr.Close()
    End Sub
I Debug and click read.

Image

It works, But lets say I want to put each line as a seperate Object in a combobox. So I replace textbox2 with combobox1.

I changed the code around to try to get it to read correctly.

Code: Select all

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Creating a StreamRead
        Dim nsr As New StreamReader("C:\WriteFile.txt")

        'Simplifying the text
        Dim txt As String = nsr.ReadToEnd()
        'Adding the text to textbox2
        TextBox2.Items.Clear()
        TextBox2.Items.Add(txt)

        'Closing the Stream Reader
        nsr.Close()
    End Sub
Image


SOLUTION CODE:

Code: Select all

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Creating a StreamRead
        Dim nsr As New StreamReader("C:\WriteFile.txt")

        'Adding Items to the Combobox
        Do Until nsr.EndOfStream
            Me.TextBox2.Items.Add(nsr.ReadLine)
        Loop

        'Closing the StreamReader
        nsr.Close()
    End Sub
Image

Anyone can use this now... Don't delete this topic.

-Click

Re: [StreamWriting/Reading] String problems

Posted: Fri Jul 03, 2009 5:26 am
by troymac1ure
Click16 wrote: It works, But lets say I want to put each line as a seperate Object in a combobox. So I replace textbox2 with combobox1.

I changed the code around to try to get it to read correctly.

Code: Select all

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Creating a StreamRead
        Dim nsr As New StreamReader("C:\WriteFile.txt")

        'Simplifying the text
        Dim txt As String = nsr.ReadToEnd()
        'Adding the text to textbox2
        TextBox2.Items.Clear()
        TextBox2.Items.Add(txt)

        'Closing the Stream Reader
        nsr.Close()
    End Sub
Image

It reads, But does not display the items as separate collections. How can I separate them?

Thanks to all who help!

-Click
Haven't worked a whole lot with VB, but you should be able to do it with .ReadLine()
Something like so:

Code: Select all

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Creating a StreamRead
        Dim nsr As New StreamReader("C:\WriteFile.txt")

        'Simplifying the text
        Dim txt As String
        TextBox2.Items.Clear()
        while ((txt = nsr.ReadLine()) != null)
        {
            'Adding the text to textbox2
            TextBox2.Items.Add(txt)
        }
        'Closing the Stream Reader
        nsr.Close()
    End Sub
Try this. If not it should be along these lines.

Re: [FIXED][StreamWriting/Reading] String problems

Posted: Fri Jul 03, 2009 5:27 am
by neodos
This is useful to me, i need to learn reading files, can you post the full source?

Thanks

Re: [FIXED][StreamWriting/Reading] String problems

Posted: Fri Jul 03, 2009 5:31 am
by JacksonCougar
Damnit, posted in the wrong one. This is all your fault Click.

Re: [FIXED][StreamWriting/Reading] String problems

Posted: Fri Jul 03, 2009 5:58 am
by neodos
Lmao, poor Click.

Re: [FIXED][StreamWriting/Reading] String problems

Posted: Fri Jul 03, 2009 7:15 am
by Click16
neodos wrote:Lmao, poor Click.
:lol:

Ok, I will put up the full source.

Source is UP!

Re: [FIXED][StreamWriting/Reading] String problems

Posted: Sun Jul 05, 2009 8:17 am
by neodos
Thanks for the source, lol the pop up messages were unnecessary though :XD:

Re: [FIXED][StreamWriting/Reading] String problems

Posted: Mon Jul 06, 2009 9:55 pm
by Click16
neodos wrote:Thanks for the source, lol the pop up messages were unnecessary though :XD:
I know, i thought it would be funny.