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
newbiewebSr. Software EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AshokCommented:
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
0
lazyberezovskyCommented:
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

0
newbiewebSr. Software EngineerAuthor Commented:
Sweet! I'll give this a try...

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

newbiewebSr. Software EngineerAuthor Commented:
I made a class called LargeRadioButton and used all your code.

The attached image is how it looks.
LargeRadioButton.png
0
lazyberezovskyCommented:
Thats because of Backcolor of your button.
Replace  g.Clear(BackColor); with  g.Clear(SystemColors.Control);
0
newbiewebSr. Software EngineerAuthor Commented:
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
0
lazyberezovskyCommented:
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

0
newbiewebSr. Software EngineerAuthor Commented:
It still not transparent
SecondTry.png
0
lazyberezovskyCommented:
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

0
newbiewebSr. Software EngineerAuthor Commented:
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
0
newbiewebSr. Software EngineerAuthor Commented:
Here's the look your code currently produces:
CurrentRadioButtons.png
0
lazyberezovskyCommented:
Have you seen my comment in code above?
// Set from designer to  Color.DarkBlue

Just copy and paste (mind your class name for constructor):


public MyRadioButton()
{
    OuterColor = Color.FromArgb(52, 101, 138);
    InnerColor = Color.FromArgb(43, 147, 43);
    Font = new Font("Arial", 8.75F);
    SetStyle(ControlStyles.SupportsTransparentBackColor, true);        
    BackColor = Color.Transparent;
}

public Color InnerColor { get; set; }
public Color OuterColor { get; set; }    

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

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

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

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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
newbiewebSr. Software EngineerAuthor Commented:
Thank you very much!

newbieweb

(you don't seem lazy to me:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.