TextBox Control (VBCCRTextBox)
VBCCRTextBox control is an enhanced text box control that provides more features than standard TextBox, including undo support, cue banner text, and balloon tips. It can be used for single-line or multi-line text input.
Properties
Key Properties
Text: Text contentSelText: Selected textSelStart: Selection start positionSelLength: Selection lengthMultiLine: Whether multi-lineScrollBars: Scroll barsAlignment: Text alignmentMaxLength: Maximum text lengthPasswordChar: Password characterUseSystemPasswordChar: Whether to use system password characterLocked: Whether lockedHideSelection: Whether to hide selection when losing focusOLEDropMode: OLE drag and drop modeCueBanner: Hint text (placeholder)CueBannerAlways: Whether to always show cue bannerTextLength: Gets the length of text (read-only)
Methods
Main Methods
Undo(): Undo last operationCanUndo(): Returns whether undo is availableResetUndoQueue(): Resets the undo queueShowBalloonTip(Text As String, Optional Title As String, Optional Icon As TxtIconConstants): Shows a balloon tip
Events
Change(): Text change eventClick(): Click eventDblClick(): Double-click eventKeyDown(KeyCode As Integer, Shift As Integer)KeyPress(KeyAscii As Integer)KeyUp(KeyCode As Integer, Shift As Integer)MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)OLEDragOver(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single, State As Integer)OLEDragComplete(Effect As Long)OLEDragStart(Data As DataObject, AllowedEffects As Long)
Code Examples
Basic Usage
vb
Private Sub Form_Load()
' Configure text box
With TextBox1
.MultiLine = True
.ScrollBars = vbVertical
.Text = "Enter text here..."
.SelStart = 0
.SelLength = Len(.Text)
.CueBanner = "Enter search keywords"
.MaxLength = 1000
End With
End SubText Processing
vb
Private Sub ProcessText()
With TextBox1
' Get selected text
Dim SelectedText As String
SelectedText = .SelText
' Replace selected text
.SelText = UCase$(SelectedText)
' Insert text at cursor position
.SelText = vbNewLine & "New Line"
' Move cursor to end
.SelStart = Len(.Text)
End With
End SubUndo Support
vb
Private Sub SetupUndo()
' The TextBox control supports undo automatically
' You can check if undo is available
mnuUndo.Enabled = TextBox1.CanUndo()
End Sub
Private Sub mnuUndo_Click()
TextBox1.Undo
End SubCue Banner Implementation
vb
Private Sub SetupCueBanner()
With TextBox1
.CueBanner = "Enter your text here..."
.CueBannerAlways = False ' Only show when empty and unfocused
End With
End SubCommon Use Cases
Password Input
vb
Private Sub SetupPasswordInput()
With txtPassword
.PasswordChar = "*"
.UseSystemPasswordChar = False
.MaxLength = 20
End With
End SubSearch Box
vb
Private Sub CreateSearchBox()
With txtSearch
.CueBanner = "Search..."
.MaxLength = 100
End With
End Sub
Private Sub txtSearch_Change()
If Len(txtSearch.Text) >= 3 Then
PerformSearch txtSearch.Text
End If
End SubBalloon Tip Validation
vb
Private Sub ValidateInput()
With TextBox1
If Len(.Text) = 0 Then
.ShowBalloonTip "Please enter some text", "Validation Error", TxtIconWarning
End If
End With
End SubReset Undo Queue
vb
Private Sub ResetUndoHistory()
TextBox1.ResetUndoQueue
End SubBest Practices
- Input Validation
vb
Private Sub ValidateInput(KeyAscii As Integer)
' Allow only numbers
If Not IsNumeric(Chr$(KeyAscii)) And _
KeyAscii <> vbKeyBack Then
KeyAscii = 0
End If
End Sub
Private Sub TextBox1_KeyPress(KeyAscii As Integer)
ValidateInput KeyAscii
End Sub- Text Change Handler
vb
Private Sub TextBox1_Change()
Static IsProcessing As Boolean
If IsProcessing Then Exit Sub
IsProcessing = True
' Process text changes
ProcessTextChanges
IsProcessing = False
End SubAdditional Tips
- Text Selection
vb
Private Sub SelectAll()
TextBox1.SelStart = 0
TextBox1.SelLength = TextBox1.TextLength
End Sub- Clipboard Operations
vb
Private Sub HandleClipboard()
' Copy to clipboard
Clipboard.Clear
Clipboard.SetText TextBox1.SelText
' Paste from clipboard
If Clipboard.GetFormat(vbCFText) Then
TextBox1.SelText = Clipboard.GetText
End If
End Sub- Get Text Length Efficiently
vb
Private Sub GetTextLength()
Dim length As Long
length = TextBox1.TextLength ' More efficient than Len(TextBox1.Text)
End Sub- Check Undo Availability
vb
Private Sub UpdateUndoButton()
mnuUndo.Enabled = TextBox1.CanUndo()
End Sub