Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Override Text Box OnPaint Method?

45 views
Skip to first unread message

Joe Keller

unread,
Aug 18, 2003, 9:50:42 PM8/18/03
to
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


Rick Spiewak

unread,
Aug 18, 2003, 11:52:33 PM8/18/03
to
Can't you just do this in the Paint event of a standard textbox?

"Joe Keller" <jos...@ameritech.net> wrote in message
news:Snf0b.27102$Vx2.12...@newssvr28.news.prodigy.com...

Joe Keller

unread,
Aug 19, 2003, 7:01:23 AM8/19/03
to
That's what I thought I was attempting to do - am I missing something here?

Joe

"Rick Spiewak" <ricks...@mindspring.com> wrote in message
news:umotTVgZ...@TK2MSFTNGP10.phx.gbl...

Joe Keller

unread,
Aug 19, 2003, 7:39:38 AM8/19/03
to
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

"Joe Keller" <jos...@ameritech.net> wrote in message

news:7sn0b.27163$Vx2.12...@newssvr28.news.prodigy.com...

Tim Wilson [MVP]

unread,
Aug 19, 2003, 7:55:34 AM8/19/03
to
Yes, OnPaint will not get called when overridden for a TextBox as this is
not supported.

--
Tim Wilson
Windows Embedded MVP

"Joe Keller" <jos...@ameritech.net> wrote in message

news:_%n0b.27170$Vx2.12...@newssvr28.news.prodigy.com...

Rick Spiewak

unread,
Aug 19, 2003, 8:36:23 AM8/19/03
to
I was thinking of just catching the Paint event of a textbox itself, without
overriding anything. But, I tried it, and the event doesn't fire on the
textbox either. :>(

"Tim Wilson [MVP]" <Tim_W...@Rogers.com> wrote in message
news:%231GdBjk...@TK2MSFTNGP12.phx.gbl...

Joe Keller

unread,
Aug 19, 2003, 8:39:38 AM8/19/03
to
Grrr...thanks! It would be nice if VisualStudio.NET gave some clue as to
this being the issue.

Joe


"Tim Wilson [MVP]" <Tim_W...@Rogers.com> wrote in message
news:%231GdBjk...@TK2MSFTNGP12.phx.gbl...

Tim Wilson [MVP]

unread,
Aug 19, 2003, 8:48:47 AM8/19/03
to
The Paint event is actually fired from the OnPaint method in the base class
so if the OnPaint method is not getting called then the event won't fire off
either. They are basically one in the same.

--
Tim Wilson
Windows Embedded MVP

"Rick Spiewak" <ricks...@mindspring.com> wrote in message
news:%230y4B6k...@TK2MSFTNGP09.phx.gbl...

0 new messages