Limited Character and Limited Line Textbox
There was a question asked if the user could limit the amount of characters per line and the amount of lines a person could input into textbox. Most of the suggestions on the web stated that you just set the max char property of the textbox and set the width/height of the textbox. That is almost the definition of a hackish technique to me and did not satisfy how I think the user wanted the control to behave. So as a result I came up with this:
Code:
Option Strict On
Option Explicit On
Public Class LimittedLineTextBox : Inherits System.Windows.Forms.TextBox
#Region "Properties"
Private currentChar As System.Int32
<System.ComponentModel.Description("Gets the current character the caret is on.")> _
Public ReadOnly Property CurrentCharacter() As System.Int32
Get
Return currentChar
End Get
End Property
Private cLine As System.Int32
<System.ComponentModel.Description("Gets the current line the caret is on.")> _
Public ReadOnly Property CurrentLine() As System.Int32
Get
Return cLine
End Get
End Property
Private maxChar As System.Int32
<System.ComponentModel.Description("Gets/Sets the maximum amount of characters per line.")> _
Public Property MaximumCharacters() As System.Int32
Get
Return maxChar
End Get
Set(ByVal value As System.Int32)
maxChar = value
End Set
End Property
Private maxLines As System.Int32
<System.ComponentModel.Description("Gets/Sets the maximum amount of lines the textbox will allow.")> _
Public Property MaximumLines() As System.Int32
Get
Return maxLines
End Get
Set(ByVal value As System.Int32)
maxLines = value
End Set
End Property
#End Region
#Region "Event Handlers"
Private Sub LimittedLineTextBox_TextChanged(sender As Object, e As System.EventArgs) Handles Me.TextChanged
currentChar = GetCaretPos()
cLine = GetCurrentLine()
If currentChar > maxChar Then SetMaxChars()
currentChar = GetCaretPos()
If Me.Lines.Length > maxLines Then SetMaxLines()
cLine = GetCurrentLine()
End Sub
#End Region
#Region "Methods"
Private Function GetCaretPos() As System.Int32
Return Me.SelectionStart - Me.GetFirstCharIndexOfCurrentLine
End Function
Private Function GetCurrentLine() As System.Int32
Return Me.GetLineFromCharIndex(GetCaretPos)
End Function
Private Sub SetMaxChars()
Me.SelectionStart = maxChar
Me.SelectionLength = Me.Lines(cLine).Length - maxChar
Me.SelectedText = String.Empty
End Sub
Private Sub SetMaxLines()
Dim tempList As System.Collections.Generic.List(Of String) = New System.Collections.Generic.List(Of String)
For i As Integer = 0 To maxLines
tempList.Add(Me.Lines(i))
Next
Me.Lines = tempList.ToArray
End Sub
#End Region
#Region "New Constructor"
Sub New()
currentChar = 0
cLine = 0
maxChar = 39
maxLines = 5
Me.WordWrap = False
End Sub
#End Region
End Class
It has a few quirks to it though:
- The WordWrap property must be set to False
- Once you've reached the maximum character and maximum line, the caret jumps to the top of the textbox at position 0.
If y'all can see any improvements on it, just let me know!