Hi all,

I am assigned with a task of creating a custom control in c# .The control should be similar to a button with on and off states.Can yo people help me out with coding ???Pls .
I donno how the control to be drawn with graphics and how to add events to it on mouse click.

Hi vaishnu, welcome at DW. :)
Consider deriving your button from an existing button. That way you get a lot of behaviour of the button for free.
Have you already some code?

No sir actually i have to design the button on my own sir .It may be like a knob control or any creative button .i dunno how to start :( :(.Can yo pls help me out !!The button should have on and off states.

commented: ++ +3

Perhaps this snippet will give you some ideas.
Ultimately all classes derive from the Object class, if this is not sufficient you have to delve deeper in the class hierarchy.

sir pls can yo tell me the difference between WPF controls and normal controls using forms ???

And here is one link to custom button control.

Thanks Sir. If i draw my control using graphics ,can i link that controls to the toolbox so that my controls can be used in other projects ??? How can i add properties to those controls ??

Yes, take a look at this example here.

commented: Great tip! +14

Sir my requirement is I have drawn a button using graphics ,and if the button is clicked I want the button shape and color to be changed.The older button should not be shown .

HERE IS MY CODING :

round buton.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
            graphics.Dispose();

        }

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

        }




    }
}

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.MouseUp+= 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);
        this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.roundButton_MouseClick);

    }
    public void roundButton_MouseClick(Object source, System.Windows.Forms.MouseEventArgs ee)
    {
        //MessageBox.Show("On state");


        Color backgroundColor = Color.Red;
        Graphics graphics = CreateGraphics();

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


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

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



    }

}

The problem in the above coding is that the first button is been shown and the new button is very big .I require the same size of the image to be maintained.

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.