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
Custom Control in C# Winforms
We can have the following types of Custom Controls in C# or VB.NET
Usercontrols
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
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
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.
C# Custom Button Control
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.
- Launch Visual Studio and Create a Windows control library project Name it as SpecialButton. By default, it will create a Usercontrol class.Delete the same since we are not creating a user control here.
- Now right click on the project and Select Add New item.Select Custom Control from the list.Name the control as SpecialButton(you can choose whatever name you want).
- Go to source code window and add the following code.
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();
}
}
}
- Build the solution successfully.Your custom control library is ready now. Feel free to customize the code if you want more affect.For example the above code works on mouse events only. Forgetting it work on tab key you need to use events like,
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#.
How to Use the Custom Control in C#
- Create a normal windows form project. Reference the above created SpecialButton Control library dll.
- You can see the SpecialButton displayed in the toolbar top left if the project inside the same solution as SpecialButton control library.
- If the control library is an independent solution and Test project is part of another solution then you need to Add the control to the toolbox.For that go to the bottom of ToolBox and Right click. Select Choose Items from the context menu.Now Go to .NET Framework Components tab.Click the Browse button and choose your library(SpecialButton) from the bin folder.
- Now you can see the custom control listed in the bottom of tool box as below.
- Drag & drop the SpecialButton to your form and set the button name and Text.I named the button as btnSpecialButton and gave caption Test as Test Button. In the button click event write the following code to ensure button work as expected.
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.
Summary
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
Leave a Reply