Hi all

I have created a custom buttom round in shape .Now i want the color of the button to be changed at the mouse click of the button .

Designer.cs

using System.Windows.Forms;
using System.Drawing;
using System;
using MyNamespace;


public class MyForm : Form
{


    public MyForm()
    {
        RoundButton roundButton = new RoundButton();
        MouseEventHandler handler = new MouseEventHandler(roundButton_MouseClick);
        roundButton.MouseClick += handler;
        roundButton.backgroundColor = System.Drawing.Color.White;
        roundButton.Size = new System.Drawing.Size(50, 50);
        roundButton.Location = new System.Drawing.Point(100, 30);
        this.Controls.Add(roundButton);

        
    }
    public void roundButton_MouseClick(Object source, System.Windows.Forms.MouseEventArgs e)
    {
       
        Color backgroundColor = Color.Red;
        Graphics graphics = CreateGraphics();

        int penWidth = 4;
        Pen pen = new Pen(Color.Green, 4);
        SolidBrush brush = new SolidBrush(backgroundColor);
        graphics.FillEllipse(brush, 0, 0, Width, Height);


        graphics.DrawEllipse(pen, (int)penWidth / 2,
          (int)penWidth / 2, Width - penWidth, Height - penWidth);

        graphics.DrawLine(pen, 26, 15, 26, 30);
        graphics.DrawArc(pen, (this.Width / 4), (this.Height / 2), (this.Width / 2), (this.Height / 4), 0, 180); // Mouth
        


    }
   

}

form.cs

using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace MyNamespace
{
    [Description("Button Control")]
    [ToolboxBitmap(typeof(Button))]
    [Designer(typeof(RoundButton))]
    public class RoundButton : UserControl
    {

        public Color backgroundColor = Color.Blue;
        protected override void OnPaint(PaintEventArgs e)
        {

            Graphics graphics = e.Graphics;

            int penWidth = 4;
            Pen pen = new Pen(Color.Yellow, 4);
            SolidBrush brush = new SolidBrush(backgroundColor);
            graphics.FillEllipse(brush, 0, 0, Width, Height);
      
            graphics.DrawEllipse(pen, (int)penWidth / 2,(int)penWidth / 2, Width - penWidth, Height - penWidth);
            graphics.DrawLine(pen, 26, 15, 26, 30);
            e.Graphics.DrawArc(pen, (this.Width / 4), (this.Height / 2), (this.Width / 2), (this.Height / 4), 0, 180); // Mouth

        }
       
        private void InitializeComponent()
        {
            
            this.SuspendLayout();
            // 
            // RoundButton
            // 
            this.Name = "RoundButton";
            //this.Load += new System.EventHandler(this.RoundButton_Load);
            this.ResumeLayout(false);

        }

        


    }
}

Here in my code what it happens is the button size is large and the old button is still visible .Its not getting repainted.Can anyone help me .Am in need of it .

Use code tags if you send code!
The use of a control's Invalidate method causes a redraw.

no sir i ve tried Invalidate() but its not working properly .......can yo tell where to place Invalidate() in my code

Can make very little out of your code.
I used this for a round button in a calculator app:

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace CalculatorApp
{
    
    // Class for "3D" oval or circle button
    // just using a color trick to simulate a 3D effect
    //
    class RoundButton : Button
    {
        private Color _startcolor = Color.White;
        private Color _endcolor = Color.Black;
        private Color _textcolor = Color.Black;
        private bool _swap = false;

        //default constructor
        public RoundButton(){}

        public RoundButton(Color startcolor, Color endcolor) 
        {
            _startcolor = startcolor;
            _endcolor = endcolor;
        }

        public Color Startcolor 
        { 
            get { return _startcolor;}            
            set { _startcolor = value;} 
        }

        public Color Endcolor
        {
            get { return _endcolor; }
            set { _endcolor = value; }
        }

        public Color Textcolor
        {
            get { return _textcolor; }
            set { _textcolor = value; }
        }

        private void DoDraw(Graphics G, bool sw) //sw does the color swap, quicker than a swap routine!
        {
            Brush textBr = new SolidBrush(_textcolor);
            Rectangle R = new Rectangle(0, 0, this.Width, this.Height);
            StringFormat strFormat = new StringFormat();
            strFormat.Alignment = StringAlignment.Center;
            strFormat.LineAlignment = StringAlignment.Center;

            G.SmoothingMode = SmoothingMode.HighQuality;    // change??????
            //G.InterpolationMode = InterpolationMode.NearestNeighbor;
            G.PixelOffsetMode = PixelOffsetMode.HighQuality;// change?????? 
            //Rectangle R2 = R;
            //R2.Inflate(-2, -2);
            GraphicsPath path = new GraphicsPath();
            path.AddEllipse(R);
            this.Region = new Region(path);

            R.Inflate(1, 1);
            //R.Inflate(R.Size);
            LinearGradientBrush linBr;
            if (sw)
                linBr = new LinearGradientBrush
                    (new Point(R.Left, R.Top), new Point(R.Right, R.Bottom), _endcolor, _startcolor);
            else
                linBr = new LinearGradientBrush
                   (new Point(R.Left, R.Top), new Point(R.Right, R.Bottom), _startcolor, _endcolor);
            //////////////
            //R.Inflate(-1, -1); 
            //Pen pen = new Pen(_startcolor); 
            //pen.Width = 1f; 
            //G.DrawEllipse(pen, R);
            //R.Inflate(1, 1);
            G.FillEllipse(linBr, R);

            R.Inflate(-5, -5);
            LinearGradientBrush linBrBack;
            if (sw)
                linBrBack = new LinearGradientBrush
                    (new Point(R.Left, R.Top), new Point(R.Right, R.Bottom), _startcolor, _endcolor);
            else
                 linBrBack = new LinearGradientBrush
                    (new Point(R.Left, R.Top), new Point(R.Right, R.Bottom), _endcolor, _startcolor);
            G.FillEllipse(linBrBack, R);

            G.DrawString(this.Text, this.Font, textBr, R, strFormat);
        }

        protected override void OnPaint(PaintEventArgs pevent)
        {
            //base.OnPaint(pevent);
            DoDraw(pevent.Graphics, _swap);    
        }

        protected override void OnMouseHover(EventArgs e) // not needed really, but nice to have
        {
            Graphics g = this.CreateGraphics();
            _swap = true;
            DoDraw(g, _swap);
            _swap = false;
            g.Dispose();
            base.OnMouseHover(e);
        }
    }
}

You could remove the double colors, and implement the color change you want in a button click event handler.

Thanks alot sir for ur code.But i dono where to run and compile this code...I mean which template or file do i have to run this coding .Should i do it with Windows Control Library ??

Drop the code I posted above in a cs file in your project.
Add the following code in your form:
As an example I put a Panel on the form and made the round button in the Panel:

public Form1()
        {           
            InitializeComponent();

            RoundButton RndBtn = new RoundButton(Color.Chocolate, Color.White);
            RndBtn.Name = "MyName";
            RndBtn.Text = "rr";
            RndBtn.Size = new Size(60, 30);
            RndBtn.Location = new Point(10, 10);            
            RndBtn.Font = new Font("Microsoft Sans Serif", 16F,
                          System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

            this.MyPanel.Controls.Add(RndBtn); //add control to a Panel in my Form
            RndBtn.Click += new EventHandler(RndBtn_Click); //RndBtn_Click to be implemented
}

Sir Can yo tell me that should i have to select Windows Control Library ,and add Custom control template and add the coding to it.Should i have to create custom controls in this manner ??Please can you help me clear with this ?????

Don't know about a Windows Control Library, you have a User Control template but I never used it. All I did was derive from the Button class and override the OnPaint method, so I could do my own drawing. It is as "simple" as that. In this thread http://www.daniweb.com/software-development/csharp/threads/201807 you will find in one of the posts how the buttons look like and what I used them for.

So can we change the button color on clicking it ..That is i need a button with on Off state ....Can i add this control to the toolbox and add properties to it..How shold i do that if so ????

sir I am very confused..can yo help me out with this ...I want to create a button with on off states with a color change...can yo help me with a coding and guide me ..i really have no idea about it please help me ...

Sir can you help me a coding to develop this control ...??

Just follow the instructions, it is all there.

using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace MyNamespace
{
    [Description("Button Control")]
    [ToolboxBitmap(typeof(Button))]
    [Designer(typeof(RoundButton))]
    public class RoundButton : UserControl
    {

        public Color backgroundColor = Color.Blue;
        protected override void OnPaint(PaintEventArgs e)
        {

            // Graphics graphics = e.Graphics;
            Graphics graphics = CreateGraphics();
            int penWidth = 4;
            Pen pen = new Pen(Color.Yellow, 4);
            SolidBrush brush = new SolidBrush(backgroundColor);
            graphics.FillEllipse(brush, 0, 0, Width, Height);

            graphics.DrawEllipse(pen, (int)penWidth / 2, (int)penWidth / 2, Width - penWidth, Height - penWidth);
            graphics.DrawLine(pen, 68, 45, 68, 80);
            e.Graphics.DrawArc(pen, (this.Width / 4), (this.Height / 2), (this.Width / 2), (this.Height / 4), 0, 180); // Mouth

        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // RoundButton
            // 
            this.Name = "RoundButton";
            this.ResumeLayout(false);

        }

        public void roundButton_MouseClick(object sender, MouseEventArgs e)
        {
            Invalidate();
            Graphics graphics = CreateGraphics();
            int penWidth = 4;
            Pen pen = new Pen(Color.Red, 4);
            SolidBrush brush = new SolidBrush(backgroundColor);
            graphics.FillEllipse(brush, 0, 0, Width, Height);

            graphics.DrawEllipse(pen, (int)penWidth / 2, (int)penWidth / 2, Width - penWidth, Height - penWidth);
            graphics.DrawLine(pen, 48, 45, 48, 80);
            graphics.DrawArc(pen, (this.Width / 4), (this.Height / 2), (this.Width / 2), (this.Height / 4), 0, 180); // Mouth

        }

    }
}

This is my program but its not working can you tell me where am wrong ????

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.