Create a custom control in .NET
Today we are going to discuss about the topic how to create a custom control in .NET. We often come across the situation to develop our own controls to use in our project. That is for the inbuilt validation or any special functionality or effects on the custom control in .NET.
Create a custom control in .NET
Even we have inbuilt controls in .NET tool box, we can develop our own custom controls. Your controls may have the different functionality based on the requirement as we don't have these features in inbuilt controls. Step by step explanation to create a Custom control in .NET
1) Launch the Microsoft Visual Studio 2010, File,New,Project
2) In the Installed templates select Visual C# ,Windows , Select the "WindowsFormsControlLibrary" template
3) Provide Name as "CtrlMyLabel",Location and Solution name in appropriate boxes ,Click on OK.
Now you can see the UserControl1.cs file which is default created on the project, delete it. We need to go to Project ,right click ,Add,select "User Control ", Select "CustomControl" from the list of the templates. The initial CustomControl page will look like as below.
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CtrlMyLabel
{
public partial class MyLabel : Control
{
public MyLabel()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
}
}
My main requirement is to create a Label control with transparent colors, fore color is Red, resizable and fit to content. Now I am working for a Transparent colors on the Label control, below is the code to achieve. Setting Fore color and Transparent feature:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CtrlMyLabel
{
public partial class MyLabel : Label
{
private Color lblcolor1 = Color.LightGreen;
private Color lblcolor2 = Color.DarkBlue;
private int lblcolor1Transparent = 64;
private int lblcolor2Transparent = 64;
public MyLabel()
{
InitializeComponent();
}
public Color cuteColor1
{
get { return lblcolor1; }
set { lblcolor1 = value; Invalidate(); }
}
public Color cuteColor2
{
get { return lblcolor2; }
set { lblcolor2 = value; Invalidate(); }
}
public int colorTransparent1
{
get { return lblcolor1Transparent; }
set { lblcolor1Transparent = value; Invalidate(); }
}
public int colorTransparent2
{
get { return lblcolor2Transparent; }
set { lblcolor2Transparent = value; Invalidate(); }
}
protected override void OnPaint(PaintEventArgs pe)
{
this.ForeColor = Color.Red;
base.OnPaint(pe);
Color c1 = Color.FromArgb(lblcolor1Transparent, lblcolor1);
Color c2 = Color.FromArgb(lblcolor2Transparent, lblcolor2);
Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, 10);
pe.Graphics.FillRectangle(b, ClientRectangle);
b.Dispose();
}
}
}
From the above code snippet, I have created cuteColor1, cuteColor2 Color properties which sets the color and for the transparent colorTransparent1, colorTransparent1 has created. Override the OnPaint() and set the transparent color and fore color of a label like as above code.
ReSizable and FitToContents features:
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
this.FitToContents();
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
this.FitToContents();
}
protected virtual void FitToContents()
{
this.Width = 200;
Size size;
size = this.GetPreferredSize(new Size(this.Width, 0));
this.Height = size.Height;
}
From the above code snippet I have override the base OnResize and OnTextChanged events to resize a control based on your requirement. FitToContents() method is to fit the content in the Label control. It means based on the text length the control will be expanded. AutoSize Feature:
[DefaultValue(false), Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public override bool AutoSize
{
get
{
return base.AutoSize;
}
set
{
base.AutoSize = value;
}
}
From the above code I have override'd the AutoSize property and allow to auto size the control. Below is the entire code to develop your custom Label Control in .NET
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CtrlMyLabel
{
public partial class MyLabel : Label
{
private Color lblcolor1 = Color.LightGreen;
private Color lblcolor2 = Color.DarkBlue;
private int lblcolor1Transparent = 64;
private int lblcolor2Transparent = 64;
public MyLabel()
{
InitializeComponent();
}
public Color cuteColor1
{
get { return lblcolor1; }
set { lblcolor1 = value; Invalidate(); }
}
public Color cuteColor2
{
get { return lblcolor2; }
set { lblcolor2 = value; Invalidate(); }
}
public int colorTransparent1
{
get { return lblcolor1Transparent; }
set { lblcolor1Transparent = value; Invalidate(); }
}
public int colorTransparent2
{
get { return lblcolor2Transparent; }
set { lblcolor2Transparent = value; Invalidate(); }
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
this.FitToContents();
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
this.FitToContents();
}
protected virtual void FitToContents()
{
this.Width = 200;
Size size;
size = this.GetPreferredSize(new Size(this.Width, 0));
this.Height = size.Height;
}
protected override void OnPaint(PaintEventArgs pe)
{
this.ForeColor = Color.Red;
base.OnPaint(pe);
Color c1 = Color.FromArgb(lblcolor1Transparent, lblcolor1);
Color c2 = Color.FromArgb(lblcolor2Transparent, lblcolor2);
Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, 10);
pe.Graphics.FillRectangle(b, ClientRectangle);
b.Dispose();
}
[DefaultValue(false), Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public override bool AutoSize
{
get
{
return base.AutoSize;
}
set
{
base.AutoSize = value;
}
}
}
}
So, you have ready with a code, by using Ctrl+Shift+B keys on your keyboard build the WindowsFormsControlLibrary. Now your .dll is ready in bin folder to use.
Create one Windows Application Project and go to Tools, Right click on the Toolbox at below and select , Choose Items. Screen shot look likes below.
Now you can select the .NET framework Components tab to add your custom control .dll file. Go to Browse and Select the .dll file which is in the location " \CtrlMyLabel\CtrlMyLabel\bin\Debug " with the name " CtrlMyLabel.dll ". The control will be added to tool box like as below.
Now you are ready to use the custom control by dragging into your winodws form. Below is the screen shot it look likes.