Creating Custom Control in C#.Net Windows Appliation

Introduction of Custom Control.
  • There are situations, when you need some specific functionality in a control, which is not provided by the built-in C#.Net controls. In such situations, you can create user controls and custom controls according to the requirement.
  • Custom control is a class, which you write and it derives form Control Classes.
Steps to Create a Custom Control

1. Add a Custom Control in your application, as CustomControl1.cs.
 

2. Select the Custom Control from the list.
 
3. Add the mentioned Code in CustomControl1.cs File.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
    public partial class CustomControl1 : Control
    {
        public CustomControl1()
        {
            InitializeComponent();
        }
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            pe.Graphics.FillEllipse(Brushes.Green, 10, 10, 100, 50);
        }
    }
}

4. Build the application. This adds the custom control in the Toolbox. As shown in the fig.
   

5. Drag and drop the CustomControl1 control on the Form.
6. Double Click the CustomControl1 control and add the code, shown below as example.
private void customControl1_Click(object sender, EventArgs e)
{
     MessageBox.Show("This is Custom Control");
}
We are displaying a message box when you click the CustomControl1 Control.
7. Press the F5 Key to run the application and click the CustomControl1 control. A message box appears as shown in the fig.

Comments

Popular posts from this blog

Write a program to check whether empty query string is entered in Asp.Net.

Mandavrayji Temple, Muli, Surendranagar, Gujarat

Write a program to change color of Label text control programmatically in Asp.Net.