Streamreader error?

rnm89

Member
Joined
Apr 1, 2003
Messages
20
Can anyone tell me whats wrong with this code? I keep getting a streamreader error.

I have added Imports System.IO at the top of the code to import the streamreader class



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

read test.txt

Dim streamtodisplay As streamreader
streamtodisplay = New streamreader("c:\test.txt")
TextBox1.Text = streamtodisplay.readtoend
streamtodisplay.close()
TextBox1.Select(0, 0)
End Sub
 

Attachments

OK, I was able to get it to work by starting a NEW project and reloading the code.

I thought however that with this code, once form1 started textbox1.text would be loaded with the data in the test.txt file.

That is not happening. I have to click into the textbox1.text box then click on the form, after a few seconds, it loads test.txt

Thanks for any help!!
 
If you want the textbox to fill on page load, move this block of code:
Code:
Dim streamtodisplay As streamreader
streamtodisplay = New streamreader("c:\test.txt")
TextBox1.Text = streamtodisplay.readtoend
streamtodisplay.close()
TextBox1.Select(0, 0)
into the formload block.

Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim streamtodisplay As streamreader
streamtodisplay = New streamreader("c:\test.txt")
TextBox1.Text = streamtodisplay.readtoend
streamtodisplay.close()
TextBox1.Select(0, 0)

End Sub

The text changed sub fires every time a character is added to your text box i.e. if you added the string "a e i o u" it will fire 9 times.
Doubt if thats what you want to do.

Probably why the delay in the fill of your text box in its current form. After you click on the text box, then click on your form, thus changing the text in the text box and starting the sub, the textbox.textchanged fires for every character in your test.txt.

Jon
 
this is the correct method to open your file and get it to fill your chosen textbox / rtfbox :
Code:
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim streamtodisplay As StreamReader = New StreamReader(New FileStream("C:\test.txt", FileMode.Open))

        With rtfBox
            .AppendText(streamtodisplay.ReadToEnd) /// add the textfile to the box
        End With

        streamtodisplay.Close()
    End Sub
 
Back
Top