Image drawn on custom button subclass does not show up

N

Nathan Baker

Hey,

I am trying to subclass a button to create a control similar to the
Firefox/IE dropdown button. My first step is to draw the 'down arrow'
on the button. I overrided the OnPaint method thusly:
protected override void OnPaintBackground(PaintEventArgs pevent)
{
base.OnPaintBackground(pevent);
paintArrow();
}

void paintArrow()
{
Graphics G = CreateGraphics();

Bitmap b = LibXIE.img.DownArrow;

//Draw the down arrow centered and to the right
Point arrowPoint = new Point(
Width - b.Width - padding,
Height / 2 - b.Height / 2);

G.DrawImageUnscaled(b, arrowPoint);
}

However, the arrow only shows up when I resize the borders of the
button (in the form designer). When the button is drawn the paintArrow
event is called (I can add debugging and see it being called), but the
arrow does not show up.

Are there other methods I have to override? Or am I doing something
wrong?

Thanks,
Nathan
 
N

Nathan Baker

Bob,

Thanks for your reply. However, I do not believe that question applies
to this case:
* I am not using a picturebox
* I am using the onpaint event
* I am not doing any drawing outside of the paint method (I do my
drawing in the drawArrow method, which is called by the paint method)

If I missed something when reading the article then please point it out
to me.

Thanks again,
Nathan
 
N

Nathan Baker

Bob,

I read over the FAQ entry again, and I noticed something I had missed
before: I was calling CreateGraphics myself instead of using the
pevent.Graphics field. I changed my code to use this instead, and it
works fine now.

Sorry for asking a question that was covered in the FAQ, and thanks for
pointing me in the right direction.

Nathan
 

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