multiline textbox question
I have a multiline textbox that has wordwrap set to True
I am assigning each line of the textbox to a string
Lets say I typed this into the textbox without pressing enter and it just wordwrapped to the next line
Visual Programming is
fun
it would assign "Visual Programming is fun" to the first string
however, what i want it to do is assign "Visual Programming is" to the first string and "fun" to the second string.....now if i would have pressed enter after "is" then it would have done what I wanted it to do, but if i dont press enter and just let it word wrap it does not do what i want it to do...
any thoughts?
Re: multiline textbox question
I would suggest that if you don't want lines to wrap then don't enable word wrap. Using the points at which a TextBox wraps lines as actual breaks in the text seems very arbitrary. If you want breaks in specific positions then you should insert breaks in specific positions.
That said, if you really want to go that way then you can make use of the GetFirstCharIndexFromLine method of the TextBox. You can use a Do Until loop to get the index of the first character of each line using that method. Once you've got all those values, you can use String.Substring to extract each line from the Text of the TextBox.
Re: multiline textbox question
You need to know how many characters can be filled on your textbox per line, then you can count the number of characters and on the Keypress event of the Textbox and then assign it to the String if it reaches your maximum count or if you pressed the enter key.
Of course you need to handle the pressing of backspace, delete, etc.
Re: multiline textbox question
Quote:
Originally Posted by
wengwashere
You need to know how many characters can be filled on your textbox per line, then you can count the number of characters and on the Keypress event of the Textbox and then assign it to the String if it reaches your maximum count or if you pressed the enter key.
Of course you need to handle the pressing of backspace, delete, etc.
That's not really practical. First up, the number of characters will vary unless you're using a fixed-width font. Also, if the user makes an edit, everything below that edit will change.
Re: multiline textbox question
Quote:
Originally Posted by
jmcilhinney
That's not really practical. First up, the number of characters will vary unless you're using a fixed-width font.
This is so true. I was noticing this when adding a textbox for code entry. Notice the different horizontal width between 7 consecutive capital letters below.
WWWWWWW
KKKKKKK
This is why you have to watch out when you add a textbox or combobox and you set the width of it. Capital W's are usually the widest letter but then there are different fonts too.
Re: multiline textbox question
Here's an implementation of the logic in post #2.
vb.net Code:
Dim charIndexes As New List(Of Integer)
Dim lineNumber As Integer = 0
Dim charIndex As Integer = Me.TextBox1.GetFirstCharIndexFromLine(lineNumber)
Do Until charIndex = -1
charIndexes.Add(charIndex)
lineNumber += 1
charIndex = Me.TextBox1.GetFirstCharIndexFromLine(lineNumber)
Loop
Dim upperBound As Integer = charIndexes.Count - 1
Dim lines(upperBound) As String
Dim text As String = Me.TextBox1.Text
For lineNumber = 0 To upperBound
charIndex = charIndexes(lineNumber)
If lineNumber = upperBound Then
lines(lineNumber) = text.Substring(charIndex)
Else
lines(lineNumber) = text.Substring(charIndex, charIndexes(lineNumber + 1) - charIndex)
End If
Next
MessageBox.Show(String.Join(Environment.NewLine, lines))