Custom Radio Button with Transparent Background

J

joshuaphillips

Hi All,

I have created a control that inherits from RadioButton and I have
overridden it's OnPaint method. It looks great, draws the background
with every color except with Color.Transparent. When I make the
controls Background color transparent, it draws it black! Is there
anyway to fix this?

Inside the OnPaint method, my first line is:
e.Graphics.Clear(Color.BackColor);

I have also set the control style SupportsTransparentBackColor to true.

Any help would be greatly appreciated.

Thanks!
 
G

Guest

Hi,
To change the controls background color to transparent you need not set
ControlStyle SupportsTransparentBackColor to true . Moreover,
inside the OnPaint method, e.Graphics.Clear(Color.BackColor); is also not
required
you can instead write

base.OnPaint(pevent); in your OnPaint method and it will work fine.


I have tried the following code and it works fine for me


public class rb : RadioButton
{

protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);

}
}


private void Form1_Load(object sender, EventArgs e)
{
rb r1 = new rb();
r1.BackColor = Color.Transparent;
this.Controls.Add(r1);

}
 
J

joshuaphillips

Thanks Swashi,

Unfortunately this doesn't work for me. The whole reason I created a
custom radio button was to be able to override its OnPaint method, and
paint and image instead of the standard circle. I wonder what the code
MS uses that paints transparency. I'll keep looking...

Josh
 
J

joshuaphillips

I actually figured it out. In my OnPaint method, I deleted the
Graphics.Clear(Color.BackColor) and just called
base.OnPaintBackground(e). That seemed to do the trick!
 

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