Extended TextBox control
- Custom got focus color
- Custom Lost focus color
- New Methods
Quick start
- Add New Windows Forms Control Library project in Visual Studio .Net 2012/15.
- Drag a Text Box to the user control
- Rename the User control as “CPTextBox” where CP stands for Code Poet.
Windows Forms Control Library Project
- Go to File – Add – New Project – Windows Form Application (name it as testProject)
- 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.
Testing the Control
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:
[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.