Can I Draw on a Tab Page?

J

Joe Keller

Hello,

I've got some code that draws a rectangle around the object passed into a
method call and this works great on a form. However, when I try to call
CreateGraphics() to get a drawing context for a tab page, the call fails.

This code works on a form:
private void DrawFocus(Control sender, Boolean on)
{
Color penColor;
Pen penDraw;

if (on == true)
penColor = Color.Red;
else
penColor = Color.White;

penDraw = new Pen(penColor);
Rectangle rt = sender.Bounds;

rt.Inflate(3,2);
// Note call to CreateGraphics() Here Works Fine
Graphics gr = this.CreateGraphics();

gr.DrawRectangle(penDraw,rt);

gr.Dispose();
penDraw.Dispose();
}

But this call fails on a tab page:
private void DrawFocus(Control sender, Boolean on)
{
Color penColor;
Pen penDraw;

if (on == true)
penColor = Color.Red;
else
penColor = Color.White;

penDraw = new Pen(penColor);
Rectangle rt = sender.Bounds;

rt.Inflate(3,2);
// Code fails Here
Graphics gr = this.tabControl1.TabPages[0].CreateGraphics();

gr.DrawRectangle(penDraw,rt);

gr.Dispose();
penDraw.Dispose();
}

Can I draw on a tab page control? Am I just not getting the correct drawing
context?

Thanks!

Joe
 
A

Alex Yakhnin, eMVP

TabPage is just a container for other controls. You can add a Panel to a
TabPage and use the Panel to draw whatever you like.
 
J

Joe Keller

Really? I'm stumped then because I tried drawing on a panel as well and it
fails on this line:
Graphics gr = panel1.CreateGraphics();

Thoughts?

Thanks,

Joe

Alex Yakhnin said:
TabPage is just a container for other controls. You can add a Panel to a
TabPage and use the Panel to draw whatever you like.

--
Alex Yakhnin, Microsoft Embedded MVP
IntelliProg, Inc.
http://www.intelliprog.com

Joe Keller said:
Hello,

I've got some code that draws a rectangle around the object passed into a
method call and this works great on a form. However, when I try to call
CreateGraphics() to get a drawing context for a tab page, the call fails.

This code works on a form:
private void DrawFocus(Control sender, Boolean on)
{
Color penColor;
Pen penDraw;

if (on == true)
penColor = Color.Red;
else
penColor = Color.White;

penDraw = new Pen(penColor);
Rectangle rt = sender.Bounds;

rt.Inflate(3,2);
// Note call to CreateGraphics() Here Works Fine
Graphics gr = this.CreateGraphics();

gr.DrawRectangle(penDraw,rt);

gr.Dispose();
penDraw.Dispose();
}

But this call fails on a tab page:
private void DrawFocus(Control sender, Boolean on)
{
Color penColor;
Pen penDraw;

if (on == true)
penColor = Color.Red;
else
penColor = Color.White;

penDraw = new Pen(penColor);
Rectangle rt = sender.Bounds;

rt.Inflate(3,2);
// Code fails Here
Graphics gr = this.tabControl1.TabPages[0].CreateGraphics();

gr.DrawRectangle(penDraw,rt);

gr.Dispose();
penDraw.Dispose();
}

Can I draw on a tab page control? Am I just not getting the correct drawing
context?

Thanks!

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