Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,

can anybody tell me the reason why the dynamically created roundbutton doesn't change its background color when initiated to do so.. The sample code is showwn below:

C#
using System.Windows.Forms;  
using System.Drawing;  
namespace MyNamespace  
{  
    public class RoundButton : UserControl  
    {  
        public Color backgroundColor = Color.LimeGreen;  
        protected override void OnPaint(PaintEventArgs e)  
        {  
            Graphics graphics = e.Graphics;  
            int penWidth = 4;  
            Pen pen = new Pen(Color.Black, 4);  
            int fontHeight = 10;  
            Font font = new Font("Arial", fontHeight);  
            SolidBrush brush = new SolidBrush(backgroundColor);  
            graphics.FillEllipse(brush, 0, 0, Width, Height);  
            SolidBrush textBrush = new SolidBrush(Color.Black);  
            graphics.DrawEllipse(pen, (int)penWidth / 2,  
            (int)penWidth / 2, Width - penWidth, Height - penWidth);  
            graphics.DrawString(Text, font, textBrush, penWidth,  
            Height / 2 - fontHeight);  
        }  
    }  
}  
  
//Usage:  
  
public class MyForm : Form  
{  
    public MyForm()  
    {  
        RoundButton roundButton = new RoundButton();  
        EventHandler handler = new EventHandler(roundButton_Click);  
        //roundButton.Click += handler;  
        roundButton.Text = "Click Here!";  
        roundButton.backgroundColor = System.Drawing.Color.LimeGreen;  
        roundButton.Size = new System.Drawing.Size(80, 80);  
        roundButton.Location = new System.Drawing.Point(100, 30);  
        this.Controls.Add(roundButton);  
    }  
}

public void button1_click(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
       {
          if (roundButton[1].backgroundColor == System.Drawing.Color.LimeGreen)
            {
                roundButton[1].backgroundColor = System.Drawing.Color.Red;
            }
          else if(roundButton[1].backgroundColor == System.Drawing.Color.Red)
            {
                roundButton[1].backgroundColor = System.Drawing.Color.LimeGreen;
            }

        }

Thanks in advance...
Posted
Updated 19-May-13 20:25pm
v3
Comments
toATwork 15-May-13 7:33am    
does it stay LimeGreen all the time? If so try to Refresh your control.
Jagadisha_Ingenious 15-May-13 10:45am    
@toATwork: Yes it remains Limegreen all the time. When debugging it enters the 1st instruction but it doesn't change. How to refresh the control...?
toATwork 15-May-13 10:46am    
Either call Invalidate() or Refresh() on the control
if (roundButton[1].backgroundColor == System.Drawing.Color.LimeGreen)
{
roundButton[1].backgroundColor = System.Drawing.Color.Red;
roundButton[1].Refresh(); // that should do the trick
}
Jagadisha_Ingenious 15-May-13 10:53am    
@toATwork: Thanks sir it worked with that instruction. But i want to know why such things happen...? What exactly is the cause for this..?
toATwork 15-May-13 10:54am    
I will post an answer with some explanation

1 solution

You need to Refresh the control because it does not know that repainting is necessary.
C#
if (roundButton[1].backgroundColor == System.Drawing.Color.LimeGreen)
{
  roundButton[1].backgroundColor = System.Drawing.Color.Red;
  roundButton[1].Refresh(); // that should do the trick
}


When you change the BackColor of a standard control, the system takes care of all that stuff for you (invalidating and repainting). But you have defined a separate property with the containing backcolor - if you change it, the control must be informed.

If you use the default BackColor propery, it should work without calling Refresh manually.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900