Results 1 to 6 of 6

Thread: multiline textbox question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2011
    Posts
    132

    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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    New Member
    Join Date
    Apr 2011
    Posts
    9

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: multiline textbox question

    Quote Originally Posted by wengwashere View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Fanatic Member EntityX's Avatar
    Join Date
    Feb 2007
    Location
    Omnipresence
    Posts
    798

    Re: multiline textbox question

    Quote Originally Posted by jmcilhinney View Post
    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.
    Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.

    "Persistence is the magic of success." Paramahansa Yogananda

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: multiline textbox question

    Here's an implementation of the logic in post #2.
    vb.net Code:
    1. Dim charIndexes As New List(Of Integer)
    2. Dim lineNumber As Integer = 0
    3. Dim charIndex As Integer = Me.TextBox1.GetFirstCharIndexFromLine(lineNumber)
    4.  
    5. Do Until charIndex = -1
    6.     charIndexes.Add(charIndex)
    7.     lineNumber += 1
    8.     charIndex = Me.TextBox1.GetFirstCharIndexFromLine(lineNumber)
    9. Loop
    10.  
    11. Dim upperBound As Integer = charIndexes.Count - 1
    12. Dim lines(upperBound) As String
    13. Dim text As String = Me.TextBox1.Text
    14.  
    15. For lineNumber = 0 To upperBound
    16.     charIndex = charIndexes(lineNumber)
    17.  
    18.     If lineNumber = upperBound Then
    19.         lines(lineNumber) = text.Substring(charIndex)
    20.     Else
    21.         lines(lineNumber) = text.Substring(charIndex, charIndexes(lineNumber + 1) - charIndex)
    22.     End If
    23. Next
    24.  
    25. MessageBox.Show(String.Join(Environment.NewLine, lines))
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width