Writing to a Multiline Textbox programmatically

A

Anil Gupte

I have read a lot about getting lines from a multiline textbox in VB.Net.
However, I cannot for the life of me figure out how to write to a multiline
textbox. Basically, I have created an array of strings which I want to
"paste" into a multiline textbox. Any ideas?
 
R

rowe_newsgroups

I have read a lot about getting lines from a multiline textbox in VB.Net.
However, I cannot for the life of me figure out how to write to a multiline
textbox. Basically, I have created an array of strings which I want to
"paste" into a multiline textbox. Any ideas?

You mean something like this:

//////////////
Dim strings As String() = {"one", "two", "three", "four"}

For Each s As String In strings
Me.TextBox1.Text += s
Me.TextBox1.Text += Environment.NewLine
Next
//////////////

Thanks,

Seth Rowe
 
A

Anil Gupte

That does seem to work. Here is what I tried:

Dim strLine As String
Dim n As Integer = 0
For Each strLine In AllLines
txtBoxSliceInfoAll(n) = strLine
n = n + 1
Next strLine

Just curious as to why that did not work

Thanx,
 
P

Phill W.

Anil said:
That does seem to work. Here is what I tried:

Dim strLine As String
Dim n As Integer = 0
For Each strLine In AllLines
txtBoxSliceInfoAll(n) = strLine
n = n + 1
Next strLine

Just curious as to why that did not work

Did it even /compile/?
If so, set Option Strict On at once!

I couldn't get this to compile; apparently, "you can't index
txtBoxSliceInfoAll because it has no default property", nor do TextBoxes
expose a property that is an array of strings making up their multi-line
content.

Build the string up with Environment.NewLine wherever you need a Line
break, just as Seth showed you:

For Each sLine As String In AllLines
txtBoxSliceInfoAll.Text = txtBoxSliceInfoAll.Text _
& sLine & Environment.NewLine
Next

or even more succinctly:

txtBoxSliceInfoAll.Text = String.Join( vbCrLf, AllLines )

HTH,
Phill W.
 
S

Stephany Young

A word of warning.

Once your 'string' gets reasonably large then setting the Text property of
the TextBox control is quite slow and after doing so the content is
'positioned' at the start.

The next question is usually, how do I move to the end off the text in my
textbox programatically.

After setting the Text Property of the TextBox you could always execute:

TextBox.SelectionStart = TextBox.TextLength
TextBox.ScrollToCaret

A better way of adding multiline data to a multiline TextBox is to use the
AppendText method. When you use this method the content automatically
scrolls to the end. The only drawback is that you habe add the NewLine
yourself:

TextBox.AppendText("Some text." & Environment.NewLine)

This makes it very easy to use a multiline textbox as an on screen 'logging'
mechanism.
 
A

Anil Gupte

Thanx, I am going to save this message and try using it later. Since we are
on the subject, how about this little problem. Occassionally, I want to
have the entire text highlighted so the users can jut start typing and
replace the text. I could not figure out how to do that.
 
S

Stephany Young

Have a look at the TextBox.SelectAll() method.


Anil Gupte said:
Thanx, I am going to save this message and try using it later. Since we
are on the subject, how about this little problem. Occassionally, I want
to have the entire text highlighted so the users can jut start typing and
replace the text. I could not figure out how to do that.
 

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