Multiline Textbox

zack

Well-known member
Joined
Jun 9, 2005
Messages
65
Programming Experience
Beginner
Hi all,

Just a noob question... How do you insert lines into a multiline textbox from some captured data? (other steps besides setting the multiline property as true)
 
Hi strider,

these are my codes for inserting the text into my textbox...

VB.NET:
 With FilteredText 

.Text = vbCrLf + "Current Location: " + Data

.SelectionStart = .Text.Length

End With

It workes in a way that whenever I click on a button to dosplay the captured text, it overwrites the previous data. What I want is to continue display the data just below the previous instead of overwriting.
 
textbox's do have an AppendText method so all you have to do is:

VB.NET:
FilteredText.AppendText("Current Location: " & Data & ControlChars.NewLine)

a side note with dealing with strings:
when adding strings together it's better to use '&' instead of '+'

i have run into problems with vb.net when using a '+' to put two strings together
 
Have you know that & operator, which is slow now has a alternative (StringBuilder)

The System.Text.StringBuilder class finally gives Visual Basic developers the tools they need to do efficient string manipulation operations. StringBuilder provides a buffer-oriented mechanism to manipulate string information. String concatenation is not its only use, but that's the most common one.

Otherwise, you might not know it, but Visual Basic cannot combine two strings. What it does is create a new string object that contains the combined source strings' values. Using the & results in a new string being created each time you use it (unless it is used in a Const statement, in which case it is evaluated at compile time). For instance, myString = "text" & "text" & "text" causes the creation of three strings just to assign one to the variables. This leads to unnecessary—or at least unwanted—memory allocation to build just one string. And it gets much worse as you add more to the string.

Regards ;)
 
kulrom said:
Have you know that & operator, which is slow now has a alternative (StringBuilder)

The System.Text.StringBuilder class finally gives Visual Basic developers the tools they need to do efficient string manipulation operations. StringBuilder provides a buffer-oriented mechanism to manipulate string information. String concatenation is not its only use, but that's the most common one.

Otherwise, you might not know it, but Visual Basic cannot combine two strings. What it does is create a new string object that contains the combined source strings' values. Using the & results in a new string being created each time you use it (unless it is used in a Const statement, in which case it is evaluated at compile time). For instance, myString = "text" & "text" & "text" causes the creation of three strings just to assign one to the variables. This leads to unnecessary—or at least unwanted—memory allocation to build just one string. And it gets much worse as you add more to the string.

Regards ;)
The use of a StringBuilder is desirable when multiple string manipulation operations are being carried out, but there is an overhead to creating the StringBuilder that does not exist when using the String. Also, you must call ToString on it when done to get a final String object. I have read on MSDN that it is more efficient to use string concatenation in most simple cases and only use the StringBuilder when concatenating many strings or several long strings. The numbers would be dependant on the situation, but the article I read said the String is as or more efficient unless you are concatenating several dozen strings or strings with several dozen characters.

With regards to operators, the "+" (addition) operator is interpreted differently depending on its arguments. If both are strings then it behaves the same way as the "&" (string concatenation) operator. If one argument is numerical, though, it will try to perform a numerical addition, which will fail. For clarity and to avoid possible errors, it is best to always use "&" when concatenating strings.

Edit:
Also, to reinforce Kulrom's point, his example actually creates 5 String objects, so sometimes your string concatenation creates many more objects than you would think.
 
Of course John. It ("&") is still useful and relatively lightweight for minimal string concatenation and i'm using it frequently as well. However, when performance is key and you have to do a lot of string concatenation, you need to look at alternatives (StringBuilder in this case).

Regards ;)

btw, you forgot to congratulate my first grand ... 1003 posts :D
 
Back
Top