Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB4/32,VB5,VB6
Expertise: beginner
Dec 4, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Show a custom caret

VB gives you no control on the size of the text caret. At times it can be necessary to change its size, however, for example when you want to signal that the TextBox is in overwrite mode. Here's a routine that does the trick:

Private Declare Function GetFocus Lib "user32" () As Long
Private Declare Function CreateCaret Lib "user32" (ByVal hWnd As Long, _
    ByVal hBitmap As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function ShowCaret Lib "user32" (ByVal hWnd As Long) As Long

' Create and show a caret of given size

Sub ShowCustomCaret(ByVal width As Integer, ByVal height As Integer)
    CreateCaret GetFocus(), 0, width, height
    ShowCaret GetFocus()
End Sub
The above routine is meant to be invoked from within the GotFocus event of all textboxes on the form. This is necessary because VB has the bad habit to destroy any caret when the focus move to another field:

Private Sub Text1_GotFocus()
    ' Show a caret 5 pixelz wide and 14 pixels tall
    ShowCustomCaret 5, 14
End Sub
Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date