Results 1 to 7 of 7

Thread: Limit Number of Lines in TextBox

  1. #1

    Thread Starter
    Registered User
    Join Date
    Apr 2006
    Posts
    62

    Limit Number of Lines in TextBox

    Hi, how do I limit the number of lines in a multiline textbox? I don't want to limit the characters, just the number of lines.

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

    Re: Limit Number of Lines in TextBox

    Handle the KeyDown event and trap the Enter key. Count the number of lines currently in the control and if it's already at the maximum then set e.SuppressKeyPress to True.
    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

    Thread Starter
    Registered User
    Join Date
    Apr 2006
    Posts
    62

    Re: Limit Number of Lines in TextBox

    Could I possibly get a code example of how this is done?

    Thanks

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

    Re: Limit Number of Lines in TextBox

    VB Code:
    1. Private Const MAX_LINE_COUNT As Integer = 10 'for example
    2.  
    3. Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    4.     If e.KeyCode = Keys.Enter Then
    5.         e.SuppressKeyPress = (Me.TextBox1.Lines.Length >= MAX_LINE_COUNT)
    6.     End If
    7. End Sub
    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
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Limit Number of Lines in TextBox

    That works so long as wordwrapping is disabled.
    VB Code:
    1. Private Const MaxLines As Int32 = 3
    2.     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    3.         dim hold As TextBox = DirectCast(sender, TextBox)
    4.             If hold.GetPositionFromCharIndex(hold.Text.Length - 1).Y > (MaxLines * hold.Font.Height) Then
    5.                 hold.Text = hold.Text.Remove(hold.Text.Length - 1)
    6.                 hold.SelectionStart = hold.Text.Length
    7.             End If
    8.     End Sub
    If you can find a way to get keyup's e.surpresskeypress to work properly (it's in the arg, just doesn't stop anything); there's a much cleaner way. Otherwise, this will work as long as the lines your textbox is displaying is greater or equal to the number of lines you're capping.

    Edit: changed a me.textbox1 to hold
    Last edited by sevenhalo; Nov 8th, 2006 at 08:43 PM.

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

    Re: Limit Number of Lines in TextBox

    Given that the KeyPress event is raised before the KeyUp event, there isn't anything to suppress. KeyDown and KeyUp both receive a KeyEventArgs object but the SuppressKeyPress was added specifically for the KeyDown event and is only useful for that event.

    Also, you DEFINITELY don't want to have a Using block like that in your event handler. The purpose of a Using block is to ensure that the object referred to by the variable declared in the Using statement gets disposed at the end of the block. I very much doubt that you want to dispose a TextBox in its own TextChanged event handler.
    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

  7. #7
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Limit Number of Lines in TextBox

    Also, you DEFINITELY don't want to have a Using block like that in your event handler. The purpose of a Using block is to ensure that the object referred to by the variable declared in the Using statement gets disposed at the end of the block. I very much doubt that you want to dispose a TextBox in its own TextChanged event handler.
    Haha, I was just about to fix that. I forgot I didn't insantiate the object.

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