Yoshi Walsh
Yoshi Walsh

Reputation: 2077

Change Caret colour or hide it VB.NET

I am making a program in VB.NET and need to hide the Caret in textboxes.

I could live with it being either completely hidden or just the same colour as the textboxes background colour. How can I go about doing this? I would prefer to stay away from Custom Controls if at all possible.

Thank you

Upvotes: 1

Views: 2547

Answers (1)

Hans Passant
Hans Passant

Reputation: 942099

Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form.

Public Class NoCaretBox
  Inherits TextBox

  Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
    HideCaret(Me.Handle)
    MyBase.OnGotFocus(e)
  End Sub

  Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)
    ShowCaret(Me.Handle)
    MyBase.OnLostFocus(e)
  End Sub

  Private Declare Function HideCaret Lib "user32.dll" (ByVal hWnd As IntPtr) As Boolean
  Private Declare Function ShowCaret Lib "user32.dll" (ByVal hWnd As IntPtr) As Boolean
End Class

Upvotes: 6

Related Questions