1
Answer

Subclassing a TextBox with UserPaint flag set....

ldragos

ldragos

20y
5.6k
1
Hello, I am trying to create a customisable - bordered TextBox control. I draw the border in the overrided method OnPaint, after I have previousely set the UserPaint style bit. I don't understand why the setting of the UserPaint flag makes the text within the textbox change its font. My example can be reduced to this : public class testTextBox : TextBox { public testTextBox() { // // TODO: Add constructor logic here // this.SetStyle(ControlStyles.UserPaint, true); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint (e); SolidBrush sb = new SolidBrush(Color.Red); Pen pen = new Pen(sb, 1); e.Graphics.DrawRectangle(pen, 0, 0, 10,10); sb.Dispose(); pen.Dispose(); } } I tried handling text drawing using DrawString(), but that dindn't work great, since the EditControl did at some point it's own painting, changing back my font. To prevent this, I overriden the OnFontChanged method, where I Invalidated my control, forcing it to redraw itself using my Paint method. The problem left was that the caret still moved within the TextBox according to the default font size (which was wider)... Can anyone help, please?
Answers (1)