Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Can anyone tell me what's 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

Posted

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!!

  • *Experts*
Posted

If you want the textbox to fill on page load, move this block of 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.

 

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 that's what you want to do.

 

Probably why the delay in the fill of your text box in it's 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

  • 1 month later...
  • Leaders
Posted

this is the correct method to open your file and get it to fill your chosen textbox / rtfbox :

   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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...