Introduction
This is a short and simple demonstration of .NET framework's capability of creating custom controls.
Here I'm going to make a custom control and then, test my control in a Windows application. I have implemented some custom properties for my control, so you can learn how it is done in C#.
Full Size Image
Building the Control
1.Open the Visual Studio and start a new project. Your project must be based on the
Windows Control Library template. Call your project ctlCuteButton and click OK.
2.Once you have your project open, delete the UserControl from the project. Just remove it because the 'User Control' is not exactly what we need here.
3.Now go to the 'Project' menu:
Project->Add User Control... and select the
Custom Control template there. 'Custom Control' is what we need in this case. You may call it
CE_CustomControl. Now click OK. A new Custom control has been added to your project.
The first thing we must do here is to change the base class of the
CE_CustomControl:
Override the following line:
public partial class CE_SampleButton : System.Windows.Forms.Control
by this one:
public partial class CE_SampleButton : System.Windows.Forms.Button
Now your control is based on the
System.Windows.Forms.Button class.
5.Now let's create some custom properties for our control. This is done by inserting the following code inside the CE_CustomControl class:
private Color _firstColor = Color.LightCyan; //first color
public Color FirstColor
{
get { return _firstColor; }
set { _firstColor = value; Invalidate(); }
}
private Color _secondColor = Color.Red; //second color
public Color SecondColor
{
get { return _secondColor; }
set { _secondColor = value; Invalidate(); }
}
/// <summary>
/// transparency degree (applies to the 1st color)
/// </summary>
private int _firstColorTransparent = 89;
public int FirstColorTransparent
{
get { return _firstColorTransparent; }
set { _firstColorTransparent = value; Invalidate(); }
}
/// <summary>
/// transparency degree (applies to the 2nd color)
/// </summary>
private int _secendColorTransparent = 80;
public int SecondColorTransparent
{
get { return _secendColorTransparent; }
set { _secendColorTransparent = value; Invalidate(); }
}
The
Invalidate() methoid is used to refresh the design view and all controls inside.
6.And the last thing to do before compiling our control is to override the
Paint event. So let's do it:
// Calling the base class OnPaint
base.OnPaint(pe);
// Create two semi-transparent colors
Color c1 = Color.FromArgb
(_firstColorTransparent, _firstColor);
Color c2 = Color.FromArgb
(_secendColorTransparent, _secondColor);
Brush b = new System.Drawing.Drawing2D.LinearGradientBrush
(ClientRectangle, c1, c2, 10);
pe.Graphics.FillRectangle(b, ClientRectangle);
b.Dispose();
7.Now you may compile the control by pressing
<Ctrl>+<Shift>+<B>.
Testing the Control
1.Open a new instance of the VS .NET. Create a new project choosing the
Windows Application template
2.From a new Windows Forms project, we can add the compiled custom control to the toolbox. I do this by right-clicking the toolbox, selecting
Customize Toolbox, and from the
.NET Framework Components tab, clicking
Browse and locating the Control Library DLL # (in our case, CE_CustomControl\bin\Debug\CE_CustomControl.dll). The component CE_CustomControl will then appear in the Toolbox.
APPLIES TO
Microsoft Visual C# .NET 2002 Standard Edition
Microsoft Visual C# 2005 Express Edition
Microsoft Visual C# 2008 Express Edition
This attachment is hidden for guests. Please log in or register to see it.
This attachment is hidden for guests. Please log in or register to see it.