mulitline textboxes

T

Thom

I'm a real newbie so I apologize for the easy question. I have academic
version of vb.net 2003.
I am trying to write a multiline textbox with data from a text file.
Properties for the textbox are multiline = true, scrollbars = both, wordwrap
= false.

I actually have two textboxes side by sied for different files and I am
using a splitter.
Here is code for reading from one file to populate the text box. I always
end up with the last line of the file.
'open the first file and display

Dim strText1 As String

If strTextFileName1 <> "" Then

Label1.Text = strTextFileName1

objTextRead1 = System.IO.File.OpenText(strTextFileName1)

Me.LeftTextbox.Text = strTextFileName1

Do Until objTextRead1.Peek = -1

strText1 = objTextRead1.ReadLine()

Me.LeftTextbox.Text = strText1

Loop

objTextRead1.Close()

End If



Any help is appreciated. Thank you. Thom
 
J

Jerry

Thom,

What you are doing is storing different values one after another to the
default or first line of the textbox. Naturally the last line stored would
be the one remaining at the end. There are several ways to do what you want,
but I think the one which you will be able to use right away is this one.
Declare a counter and increase it in your loop and use the counter to access
the "Lines" property of the textbox. Also there are better ways to determine
that you have read a line and better ways to detect end of file, but I won't
go into them.

Like so...

Dim strText1 As String

If strTextFileName1 <> "" Then

Label1.Text = strTextFileName1

objTextRead1 = System.IO.File.OpenText(strTextFileName1)

Me.LeftTextbox.Text = strTextFileName1

Dim ACounter as integer = 0

Do Until objTextRead1.Peek = -1 'Check out the while loop and the .Read
property

strText1 = objTextRead1.ReadLine()

Me.LeftTextbox.Lines(ACounter) = strText1
ACounter = ACounter + 1
Loop

objTextRead1.Close()

End If
 
H

Herfried K. Wagner [MVP]

* "Thom said:
I actually have two textboxes side by sied for different files and I am
using a splitter.
Here is code for reading from one file to populate the text box. I always
end up with the last line of the file.
'open the first file and display

Dim strText1 As String

If strTextFileName1 <> "" Then

Label1.Text = strTextFileName1

objTextRead1 = System.IO.File.OpenText(strTextFileName1)

Me.LeftTextbox.Text = strTextFileName1

Do Until objTextRead1.Peek = -1

strText1 = objTextRead1.ReadLine()

Me.LeftTextbox.Text = strText1

Replace the line above with 'Me.LeftTextBox.Text &= strText1'.
 
T

Thom

Herfried K. Wagner said:
Replace the line above with 'Me.LeftTextBox.Text &= strText1'.

Thanks Gents I appreciate the help. I used Herfried's example. Works like a
champ.
Thanks again.
Thom
 
H

Herfried K. Wagner [MVP]

* "Thom said:
Thanks Gents I appreciate the help. I used Herfried's example. Works like a
champ.

Notice that, for large files, using a 'StringBuilder' for building the
string and then assinging its 'ToString' return value to the textbox's
'Text' property will be faster.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top