Override Text Box OnPaint Method?

J

Joe Keller

Hello,

I'm attempting to override a text box control to draw a custom border around
the control when the control has the focus using this snippet of code:

public class MyTextBox: System.Windows.Forms.TextBox
{
protected override void OnPaint(PaintEventArgs e)
{
Graphics gr = e.Graphics;
using (Pen penDraw = new Pen(Color.Red))
{
gr.DrawRectangle(penDraw,0,0,this.Width-4,this.Height-4);
}

//base.OnPaint(e);
}
}


If I inherit from Systems.Windows.Forms.Control the rectangle is drawn as
expected. However, if I inherit from the TextBox class (as shown above),
nothing is drawn - why?

I really would not like to build up all of the functionality of a text box
myself from a base "Control" class but would rather take all of the
functionality currently in a TextBox control and just extend it a little.

Any help appreciated!

Thanks,

Joe
 
J

Joe Keller

A quick update...it appears that the OnPaint() event is not getting called
(I set a breakpoint and it never breaks) when I inherit from "TextBox".

However, if I use the same code and instead inherit from "Control" the
OnPaint() event fires correctly.

Any suggestions?

Thanks,

Joe
 
T

Tim Wilson [MVP]

Yes, OnPaint will not get called when overridden for a TextBox as this is
not supported.
 
J

Joe Keller

Grrr...thanks! It would be nice if VisualStudio.NET gave some clue as to
this being the issue.

Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top