Custom controls are specialized controls in .NET derived from Control class. In this article, we explain the step by step process of creating a custom control in C# WinForms application.
Table of Contents
We can have the following types of Custom Controls in C# or VB.NET
Usercotrols are the simpler form of custom controls which are derived from the class
System.Windows.Forms.UserControl. In most cases, UserControls form a compositional approach in which multiple controls are grouped together in a single user interface element.
Example, Login control with username and password text boxes.
Inherited controls are an extension of an existing control. First you find the preexisting.NET control that most closely matches your desired functionality. Then you inherit that class with extended features such as additional behaviours and properties.
Example of Inherited controls are Cutom button control(explained below) , ExtendedRichTextBox Control, etc.
Owner-drawn controls normally draw user interfaces from beginning using GDI+ routines. They inherit the System.Windows.Forms.Control class. Because they are built up from scratch the Owner-drawn controls need the most effort to make , but they can offer the most adaptable user interface.
Extender providers
These widgets are a fantastic method of creating an adaptable user interface because they extend the functionality of existing controls on a form.
In this post, we are going to see how can we creating custom controls. As an example, we will create a custom button control. The example is a Specialized C# button control which changes its look and feels when mouse hovers over it.
If not active the button will appear as shown in the below screenshot.
When you hover mouse over the button(focus on the button) the look and feel of the button changes as below and the cursor type also change to hand type.
Now let’s see the steps involved in creating this special button control.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace SpecialButton
{
public partial class SpecialButton : Button
{
public SpecialButton()
{
InitializeComponent();
//Set default property values for the button during startup
SetNormalValues();
}
/// <summary>
/// To Set button properties when not active.i.e when button not in focus.
/// </summary>
private void SetNormalValues()
{
this.Font = new Font("Verdana", 8F, FontStyle.Bold);
this.BackColor = Color.Gray;
this.ForeColor = Color.White;
this.Margin = new Padding(4, 1, 4, 1);
this.Padding = new Padding(4);
this.MinimumSize = new Size(150, 35);
this.Cursor = Cursors.Arrow;
}
/// <summary>
/// Set attributes to highlight button when it is under focus/active.
/// Change the cursor also as Hand type
/// </summary>
private void SetValuesOnFocus()
{
//Increase the font size and colors on focus
this.Font = new Font("Verdana", 10F, FontStyle.Bold);
this.BackColor = Color.Green;
this.ForeColor = Color.White;
//Set the cursor to Hand type
this.Cursor = Cursors.Hand;
}
/// <summary>
/// Default handler.Nothing to do here since we don't need to repaint the button.
/// </summary>
/// <param name="pe"></param>
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
/// <summary>
/// Event handler which call SetValuesOnFocus() method to give apecial
/// effect to button while active
/// </summary>
/// <param name="e"></param>
protected override void OnMouseHover(EventArgs e)
{
base.OnMouseHover(e);
SetValuesOnFocus();
}
/// <summary>
/// Event handler which call SetNormalValues() method to set back the button
/// to normal state
/// </summary>
/// <param name="e"></param>
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
SetNormalValues();
}
}
}
protected override void OnEnter(EventArgs e)
{
base.OnEnter(e);
SetValuesOnFocus();
}
protected override void OnLeave(EventArgs e)
{
base.OnLeave(e);
SetNormalValues();
}
For the time being, we will concentrate on the above-mentioned code with mouse events to understand the basics of creating custom control in C#.
private void btnSpecialButton_Click(object sender, EventArgs e)
{
MessageBox.Show("C# Custom button click test");
}
Run your WinForm project Test the button.On mouse enter t the button you get the highlighted effect with Hand cursor.
On Click, you get the message box.
On mouse leave the button go back to default state.
This post covered the steps to create custom controls in C# WinForms.The code logic is same for Creating Custom controls in VB.NET also.Hope this article was helpful for you.If you have any queries or feedback to share about Creating Custom controls in .NET, write it in the comments section below.
Related Searches: Make Custom control in C# and VB.NET, Create custom control in C# and VB.NET
You may also be interested to read Create Usercontrol in C# WinForms applications
OWIN (Open Web Interface for .NET) is an interface between web servers and web applications…
JSON (JavaScript Object Notation) is a commonly used data exchange format that facilitates data exchange…
The CAP theorem is also known as Brewer's theorem. What is CAP Theorem? CAP theorem…
Some of the Key factors that need to consider while architecting or designing a software…
The Interface Segregation Principle (ISP) is one of the SOLID principles of object-oriented design. The…
The Single Responsibility Principle (SRP), also known as the Singularity Principle, is a software design…