Create custom form controls in C#.Net


.Net frame provide rich set of control to design  application GUI with a drag and drop actions. Sometime we love to have extend these control features. This can be achieved using Windows Forms Control Library Projects. Let’s learn how to do.

Extended TextBox control

Our extended control have following features
  • Custom got focus color
  • Custom Lost focus color
  • New Methods
Using the new Text Box you can enable different Leave/Enter colors. The example look like simple but it will demonstrate how you can add new properties for using at run time and design time.

Quick start

  1. Add New  Windows Forms Control Library project in Visual Studio .Net 2012/15.
  2. Drag a Text Box to the user control
  3. Rename the User control as “CPTextBox” where CP stands for Code Poet.
    Windows Forms Control Library Project
Now we need a Windows Application to test our CPTextBox, add a new project by,
  1. Go to File – Add – New Project – Windows Form Application (name it as testProject)
  2. Go to Solution Explorer – There is your two project, right click the Windows Application Project, and choose Set as Start Up Project.

We are almost ready to make changes to our custom control. Double click our custom control  and the following methods and drop properties to our code.

namespace CodePoetControls
{

public partial class CPTextBox: UserControl
{
Color Ecolor;
Color Lcolor;

public CPTextBox()
{
InitializeComponent();
textBox1.BackColor = Color.White;

}

private void textBox1_Enter(object sender, EventArgs e)
{
textBox1.BackColor = Ecolor;
}
private void textBox1_Leave(object sender, EventArgs e)
{
textBox1.BackColor = Lcolor;
}

public string GetText()
{
return (textBox1.Text);
}

[Browsable(true)]
[Category(“Extented Properties”)]
[Description(“Get input Text”)]
[DisplayName(“BoxText”)]
public string BoxText
{
set
{

textBox1.Text = value.ToUpper().Trim();

}
get
{
return (textBox1.Text  );
}
}
[Browsable(true) ]
[Category(“Extented Properties”)]
[Description (“Set Focus Color”)]
[DisplayName(“Enter Color”)]
public Color  EnterColor
{
set
{
Ecolor  = value ;
base.OnEnter(new EventArgs() );
}
get
{
return (Ecolor );

}
}

[Browsable(true)]
[Category(“Extented Properties”)]
[Description(“Set Lost Focus Color”)]
[DisplayName(“Leave Color”)]
public Color LeaveColor
{
set
{
Lcolor  = value;

}
get
{
return (Lcolor );
}
}

}
}

Compiling the User Control project.

After you successfully add the necessary code portion and ready to make your control usable. Right click the Custom control project and Build. It will generate necessary .dll files for your project.

Testing the Control

For using the control , go to the second project, open the Form. The Tool Box will show your new Control, just drag and drop CPTextBox.

The Properties Windows Categorized view will showcased your new Controls Extended Properties and try to change the Leave Color and Enter Color respectively.

If you wish to have more changes to the control, go to the custom control project and make necessary changes and Rebuild.

Distributing and using the control

Distributing and using the Dll file. For working with the new control you only need the compiled Dll not the custom project. You can add it by

  • Right click Tool Box – Choose Items
  • Brow the Dll of your custom control
  • Enable the control and it will be available on your control box.

Note:

The following code snippet will make your properties available at design time and can be accessible by using Properties.
        [Browsable(true)]
        [Category(“Extented Properties”)]
        [Description(“Set Lost Focus Color”)]
        [DisplayName(“Leave Color”)]

Using the existing Property name as Display Name will hide the default, for example if you use Text as Display Name for your property and the default property will be discarded and will be unavailable.

That’s all I have today
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.