Link to home
Start Free TrialLog in
Avatar of Brian
BrianFlag for United States of America

asked on

How to break up a word wrapped textbox for output in a VB.net program

I have a multiline textbox that could word wrap (user could type in a paragraph of text).  The problem is, when a line of text word wraps, I have no idea when that happens.  And because of this, when I output the line, it may run off the screen.  I have a string array that I am adding my output to, then when all lines are added, I will loop thru the array outputting the lines.

What I would like to do for the multiline texbox  is check if the string is more than 155 characters.  If the string is over 155 characters, I would break it at the first space that exists prior to the 156th character and add the text before that space to the array.  The remaining text would need to be tested to see if it's longer 155 characters.  If it is, it would need to be broken up the same way, if not, the remaining characters would be added to the next element in the array.

The code would look something like this...
dim strAnswer1 as string = texbox1.text
if strAnswer1.length > 155 Then
   add first part of string to strArray(i)
   handle remaining text in strAnswer1
else
   strArray(i) = strAnswer1
end if

Open in new window

I think there would need to be a loop where I would increment the "i" variable, which is the count, but I'm unclear how I should do this.  Basically what I need to do is breakup a string that might be longer than 155 characters (could be 500+ characters) into lengths less than 156 characters and add them to a string array, but I need the break to be at a point where there is a space.  Thanks!
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Brian

ASKER

Wow HainKurt, that's awesome... so concise!  I'm going to need to keep track of what line I'm on in the string array (the count) and also keep track of the string array.  I know how to accomplish that within the sub you created, I'm just unsure of how a professional programmer would handle getting that information back to the calling sub.  I'm thinking the best way is to pass both the string array and the count as ByRef parameters to the sub?
Avatar of Brian

ASKER

Thank you so much for your expertise!