Click here to Skip to main content
12,887,625 members (33,021 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 31 Oct 2005

Textbox Line Character Limit

, 31 Oct 2005
Rate this:
Please Sign up or sign in to vote.
Limit the number of characters in a multi line textbox.


Recently, while I was working on a project, there was a need to limit the amount of text in each line of a multi line textbox to 72 characters. The challenge was to limit the text while the user is typing and to maintain the required formatting. To get this to work, I had to create a class that inherited from TextBox. In the constructor, I placed a global keyboard trap on the TextBox. When the text reaches the designated length the program will stop all keyboard characters from displaying on the screen forcing the user to hit the enter key and start a new line.

The code

In the constructor, the program calls the function HookKeyboard. In this function, the program will trap all keyboard events for the TextBox:

Public Sub New()
    'This call is required by the Windows Form Designer.
    'Hook all Keyboard Events
End Sub

The function isHooked in the Keyboard module, where the actual fun begins:

'This function is where you capture the key pressed. 
Public Function IsHooked(ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean
    Dim zKey As Keys
    Dim control As Control
    Dim hInstance As Integer
        Dim ActiveWin As Integer
        'GetActiveWindow determines if the application has focus
        ActiveWin = GetActiveWindow
        If ActiveWin <> 0 Then
            If UBound(frmM.Lines) >= 0 Then
                If frmM.Lines(GetTextBoxCurrentLine(frmM) - 1).Length > _
                                                          MaxLengthLine Then
                'Accept Enter Button
                    If (Hookstruct.vkCode = zKey.Enter) Then
                        Return True
                    End If
                    'Accept BackSpace
                    If (Hookstruct.vkCode = zKey.Back) Then
                        Return True
                    End If
                    'Accept Up Arrow
                    If (Hookstruct.vkCode = zKey.Up) Then
                        Return True
                    End If
                    'Accept Down Arrow
                    If (Hookstruct.vkCode = zKey.Down) Then
                        Return True
                    End If
                    'Accept Left Arrow
                    If (Hookstruct.vkCode = zKey.Left) Then
                        Return True
                    End If
                    'Accept Right Arrow
                    If (Hookstruct.vkCode = zKey.Right) Then
                        Return True
                    End If
                    Return True
                End If
            End If
        End If
        Return False
    Catch ex As Exception
        MsgBox(ex.Message + vbCrLf + ex.StackTrace)
        Return False
    End Try
End Function


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Ryan Rader
Web Developer
United States United States
Specialize in interfacing SAP and .net applications.

You may also be interested in...

Comments and Discussions

QuestionInsert in data gride Pin
jayesht31-Mar-07 15:24
memberjayesht31-Mar-07 15:24 
Questioncontrol doesnt work when using two on the same form Pin
zastremi29-Aug-06 10:54
memberzastremi29-Aug-06 10:54 
AnswerRe: control doesnt work when using two on the same form Pin
rader1919-Sep-06 13:16
memberrader1919-Sep-06 13:16 
GeneralRe: control doesnt work when using two on the same form [modified] Pin
manfred5119-Nov-06 23:06
membermanfred5119-Nov-06 23:06 
GeneralTake care of with the width of the textbox Pin
JorgeRuiz11-Aug-06 11:02
memberJorgeRuiz11-Aug-06 11:02 
QuestionCan I use this with my browser Pin
nachtegaal999925-Apr-06 9:37
membernachtegaal999925-Apr-06 9:37 
GeneralPainting a Warning Line Pin
Ian MacLean6-Dec-05 14:35
memberIan MacLean6-Dec-05 14:35 
GeneralRe: Painting a Warning Line Pin
rader197-Dec-05 2:06
memberrader197-Dec-05 2:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 31 Oct 2005
Article Copyright 2005 by Ryan Rader
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid