Custom Radio Button with Transparent Background

  • Thread starter Thread starter joshuaphillips
  • Start date Start date
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!
 
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);

}
 
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
 
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!
 
Back
Top