Reader Level:
Articles

Super Button Custom Control in C#

By Sapna on October 17, 2011
Now you see how to creating a Super Button Custom Control in C#, which will appear Large whenever we move the Cursor on the Button.
  • 0
  • 0
  • 21461

Introduction:

Now we are going to discuss how to create a custom control in C#. How dp we create and use them in  a windows application ? As we know, a custom control is a control made by the user for a specific purpose and later we can use it in our application. In this article we are going to see how we create and implement a custom window control. Now we are going to make a Super Button by inheriting the Button class. It will appear as a super button whenever we place the cursor on the button control. It will appear just like a huge button in size. Whenever we mouse over on the button it just change its size and becomes very large.

Let see how to create and use it:

Step 1: Now take a Class Library

  • First go to File->New->Project->Class Library

step_1.gif

  • After that add some references

  • First right click on Class Library project reference property and add some references

  • Figure is given below how to add the library and after that write at the top of the Class library

step_1_1.gif

  • Now add the some namespaces to the Class Library

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

Step 2: Now Drag and Drop the button control to the SupButton.cs[Design] file as shown in the below figure.

after_step2.gif

Step 3: Code of Class named SupButton and inherits from the Button Class.

Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
namespace ClassLibrary2
{
  public class SupButton1:Button
  {
      private Button button1;
      private bool isSuper = true;
      [Description("Gets or sets whether or not the control is Super")]
      [Category("Super Stuff")]
      [DefaultValue(true)]
      public bool IsSuper
     {
         get { return isSuper; }
         set { isSuper = value; }
     }
     protected override void OnMousePlaced(EventArgs e)
     {
         this.Size = new Size(150, 150);
         base.OnMousePlaced(e);
     }
    private void InitializeComponent()
    {
        this.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
         //
        // button1
       //
       this.button1.Location = new System.Drawing.Point(0, 0);
       this.button1.Name = "button1";
       this.button1.Size = new System.Drawing.Size(75, 23);
       this.button1.TabIndex = 0;
       this.button1.Text = "button1";
       this.button1.UseVisualStyleBackColor = true;
       this.ResumeLayout(false);
    }
  }
}

Code Description : Here we are using a class named SupButton1 which inherits from the Button Class. Here we are making a property named IsSuper and by default it will return true. [Description] it shows the description of the property or event. [Category] shows the Category for which they are grouped. Further we have to make a method named OnMousePlaced(EventsArgs e) which will fire an event if the mouse is placed over the button and also increase it's size by calling the size class of the drawing namespace and it's size become larger.  Further the initialize component shows whatever the setting we have implemented for a button that is all about the code.

Step 4: Now after doing that all, Build the solution.

Step 5: Now open a new Window Forms Application project, as shown in the below figure.

after_step_5.gif

Step 6: After opening the windows application, follow the sequence given below.

  • Now Right click on Solution Explorer and add Existing project

after_step_6_1.gif

  • Now Right click on window application project  and add a reference of the Classlibrary.dll.  The below figure shows both projects

after_step_6_2.gif

Step 7: Now you will see that it will appear in to the toolbox component like below.

after_step_7.gif

Step 8: Now you want to add the control into the Toolbox then click on the any toolbox item and select Choose item and add the .NET Component reference to the project . How it will happen see in the figure given below:

after_step_8.gif

Step 9: Now drag and drop the sup Button Control to the window form and run it by pressing F5.

Before Run:

Before_run.gif

After Run:

Output.gif

COMMENT USING

Trending up