devxlogo

Show a custom caret

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 LongPrivate Declare Function CreateCaret Lib "user32" (ByVal hWnd As Long, _    ByVal hBitmap As Long, ByVal nWidth As Long, ByVal nHeight As Long) As LongPrivate Declare Function ShowCaret Lib "user32" (ByVal hWnd As Long) As Long' Create and show a caret of given sizeSub 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, 14End Sub

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist