Creating Custom Control in C#.Net Windows Appliation
Introduction of Custom Control.
5. Drag
and drop the CustomControl1 control on the Form.
- 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.
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.
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