Just for completeness, I needed such a functionality for using with a DevExpress WinForms TextEdit
control.
They already do provide a ShowCaret
and a HideCaret
method, unfortunately they are protected. Therefore I created a derived class that provides the functionality. Here is the full code:
public class MyTextEdit : TextEdit
{
private bool _wantHideCaret;
public void DoHideCaret()
{
HideCaret();
_wantHideCaret = true;
}
public void DoShowCaret()
{
ShowCaret();
_wantHideCaret = false;
}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
if (_wantHideCaret)
{
HideCaret();
}
}
}
To use the code, simply use the derived class instead of the original TextEdit
class in your code and call DoHideCaret()
anywhere, e.g. in the constructor of your form that contains the text edit control.
Maybe this is helpful to someone in the future.