Overriding Textbox with SetStyle breaks it
Posted on 2006-11-19
The goal is to build a custom TextBox, which permits drawing a bold box around it, to accentuate the box.
To do this, it is necessary to override the OnPaint event.
The onPaint event only fires if SetStyle is called in the control's constructor.
The problem is that the custom textbox doesn't display the same font, nor does it display the value as design time.
And I don't want to reinvent the wheel by reimplementing the standard textbox drawing of standard text.
If you use the designer to add a standard TextBox and MyTextBox controls, you will see the differences in behavior.
What is necessary, so that OnPaint may be called, but otherwise standard TextBox behavior is used?
Public Class MyTextBox
Public Sub New()
SetStyle(ControlStyles.UserPaint, True) ' enable OnPaint call
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)