Link to home
Start Free TrialLog in
Avatar of curiouswebster
curiouswebsterFlag for United States of America

asked on

Overriding the Paint Event for a Radio Button

I am trying to make a Radio Button draw itself larger. But changing the font size did nothing. So I was told I might be able to do this by overridding the Paint method of RadioButton.

But this websits has the code in VB only.

http://msdn.microsoft.com/en-us/library/cksxshce%28VS.71%29.aspx

Could someone please give me a hand and also tell me if this method might allow me to make teh radio buttons with a larger diameter?

Thanks,
newbieweb
Avatar of Ashok
Ashok
Flag of United States of America image

It is surprising that if you click on C#, there is no code.

But if you click on VB and scroll down, C# code is just below VB.NET code.

I will try to find solution for this.

Ashok
You should inherit from RadioButton and override OnPaint event.
Below is very hardcoded example of big radiobutton. Refactor this, or use as is :)
public class MyRadioButton : RadioButton
{
    protected override void OnPaint(PaintEventArgs pevent)
    {            
        Graphics g = pevent.Graphics;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        g.Clear(BackColor);
        
        int diameter = ClientRectangle.Height - 1;
        
        RectangleF innerRect = new RectangleF(1F, 1F, diameter-2, diameter-2);
        g.FillEllipse(new SolidBrush(Color.White), innerRect);
        g.DrawEllipse(new Pen(SystemBrushes.Control), innerRect);

        Rectangle outerRect = new Rectangle(0, 0, diameter, diameter);
        g.DrawArc(new Pen(Color.White), outerRect, -45, 180);
        outerRect.Inflate(-1, -1);
        g.DrawArc(new Pen(SystemColors.ControlDarkDark, 2F), outerRect, 135, 180);
        g.DrawArc(new Pen(SystemColors.Control, 2F), outerRect, -45, 180);
        
        if (Checked)
        {
            innerRect.Inflate(-3, -3);
            g.FillEllipse(new SolidBrush(Color.Black), innerRect);
        }

        g.DrawString(Text, Font, new SolidBrush(ForeColor), diameter + 5, 1);
    }
}

Open in new window

Avatar of curiouswebster

ASKER

Sweet! I'll give this a try...

I made a class called LargeRadioButton and used all your code.

The attached image is how it looks.
LargeRadioButton.png
Thats because of Backcolor of your button.
Replace  g.Clear(BackColor); with  g.Clear(SystemColors.Control);
That helped, but I really do need clear, since it's over a fading background.

Attached is the way I am supposed to make the buttons look. Your code looks so complicated to me I don't know if I could even modify it to create this look.


RadioButtons.png
Also you can create to images and set em for checked or unchecked state.
// Set from designer to  Color.DarkBlue
public Color BorderColor { get; set; }    

protected override void OnPaint(PaintEventArgs pevent)
{            
    Graphics g = pevent.Graphics;
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    g.Clear(BackColor);
    
    int diameter = ClientRectangle.Height - 1;
    
    RectangleF innerRect = new RectangleF(1F, 1F, diameter-2, diameter-2);
    g.FillEllipse(new SolidBrush(Color.White), innerRect);
    g.DrawEllipse(new Pen(BorderColor), innerRect);

    Rectangle outerRect = new Rectangle(0, 0, diameter, diameter);
    g.DrawEllipse(new Pen(BorderColor), outerRect);
    
    if (Checked)
    {
        innerRect.Inflate(-3, -3);
        g.FillEllipse(new SolidBrush(Color.LimeGreen), innerRect);
    }

    g.DrawString(Text, Font, new SolidBrush(ForeColor), diameter + 5, 1);        
}

Open in new window

It still not transparent
SecondTry.png
Replace g.Clear(BackColor);
with PaintParentBackground(pevent);

and add method itself:
private void PaintParentBackground(PaintEventArgs e)
{
    if (Parent == null)
    {
        e.Graphics.FillRectangle(SystemBrushes.Control, ClientRectangle);
        return;
    }


    Rectangle rect = new Rectangle(Left, Top, Width, Height);
    e.Graphics.TranslateTransform(-rect.X, -rect.Y);

    try
    {
        using (PaintEventArgs pea = new PaintEventArgs(e.Graphics, rect))
        {
            pea.Graphics.SetClip(rect);
            InvokePaintBackground(Parent, pea);
            InvokePaint(Parent, pea);
        }
    }
    finally
    {
        e.Graphics.TranslateTransform(rect.X, rect.Y);
    }

}

Open in new window

Awesome!

I wish I could say I understood how to make these last changes, but I don't.

The center color needs to be RGB: 43, 147, 43
The diameter of the inner circle needs to be a little smaller
There needs to be a circle around the button, RBG: 52, 101, 138

Thanks for all the help. Really really appreciate it.

newbieweb
RadioButtons.png
Here's the look your code currently produces:
CurrentRadioButtons.png
ASKER CERTIFIED SOLUTION
Avatar of lazyberezovsky
lazyberezovsky
Flag of Belarus image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much!

newbieweb

(you don't seem lazy to me:)