TextBox Control (VBCCRTextBox)
VBCCRTextBox control is an enhanced text box control that provides more features than the standard TextBox, including undo/redo, auto-complete, spell checking, and more. 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 characterLocked
: Whether lockedHideSelection
: Whether to hide selection when losing focusOLEDropMode
: OLE drag and drop modeCueBanner
: Hint text (placeholder)AutoComplete
: Auto-completeUndoLimit
: Undo limit
Methods
Main Methods
SelAll()
: Select all textCut()
: CutCopy()
: CopyPaste()
: PasteUndo()
: UndoRedo()
: RedoLoadFile(FileName As String)
: Load fileSaveFile(FileName As String)
: Save fileFindText(Text As String, [Start As Long], [Options As SearchOptionsConstants])
: Find text
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)
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 Sub
Text 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 Sub
Undo/Redo Support
vb
Private Sub SetupUndoRedo()
With TextBox1
.UndoLimit = 100 ' Set undo steps
' Add edit menu items
mnuEdit.Enabled = True
mnuUndo.Enabled = True
mnuRedo.Enabled = True
End With
End Sub
Private Sub mnuUndo_Click()
If TextBox1.CanUndo Then TextBox1.Undo
End Sub
Private Sub mnuRedo_Click()
If TextBox1.CanRedo Then TextBox1.Redo
End Sub
Auto-Complete Implementation
vb
Private Sub SetupAutoComplete()
With TextBox1
.AutoComplete = True
.AutoCompleteSource = acSourceCustom
' Add custom auto-complete items
.AutoCompleteCustomSource.Clear
.AutoCompleteCustomSource.Add "Visual Basic"
.AutoCompleteCustomSource.Add "VB6"
.AutoCompleteCustomSource.Add "VBScript"
.AutoCompleteCustomSource.Add "Visual Studio"
End With
End Sub
Common Use Cases
Search Box
vb
Private Sub CreateSearchBox()
With txtSearch
.CueBanner = "Search..."
.Height = 20
.Width = 150
' Add search icon
Set .Picture = LoadPicture("search.ico")
.PicturePosition = sbrLeft
' Enable real-time search
.EnableRealTimeSearch = True
End With
End Sub
Private Sub txtSearch_Change()
If Len(txtSearch.Text) >= 3 Then
PerformSearch txtSearch.Text
End If
End Sub
Rich Text Editor
vb
Private Sub CreateEditor()
With TextBox1
.MultiLine = True
.ScrollBars = vbBoth
.Font.Name = "Consolas"
.Font.Size = 10
' Set up line numbers
.ShowLineNumbers = True
.LineNumberColor = vbBlue
' Enable syntax highlighting
.EnableSyntaxHighlighting = True
.HighlightColor = vbRed
End With
End Sub
Best 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 Sub
Known Issues and Solutions
- Memory Management
vb
Private Sub ClearLargeText()
' Clear large text efficiently
TextBox1.Text = vbNullString
TextBox1.Refresh
DoEvents
End Sub
- Performance Optimization
vb
Private Sub OptimizePerformance()
' Suspend updates
TextBox1.LockUpdate = True
' Perform multiple operations
ProcessLargeText
' Resume updates
TextBox1.LockUpdate = False
TextBox1.Refresh
End Sub
Additional Tips
- Text Selection
vb
Private Sub SelectWord()
Dim Start As Long
Dim Length As Long
With TextBox1
Start = .SelStart
Length = .SelLength
' Find word boundaries
While Start > 0 And Mid$(.Text, Start, 1) <> " "
Start = Start - 1
Wend
While Start + Length < Len(.Text) And _
Mid$(.Text, Start + Length, 1) <> " "
Length = Length + 1
Wend
' Select word
.SelStart = Start
.SelLength = Length
End With
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
- File Operations
vb
Private Sub TextFileOperations()
' Save to file
TextBox1.SaveFile App.Path & "\output.txt"
' Load from file
TextBox1.LoadFile App.Path & "\input.txt"
End Sub