Today I had a situation in my project were I need a textbox control with custom border property. Microsoft .Net framework doesn’t allow us to render a textbox with custom border color; you can only change the appearance of the textbox control from Fixed3D to flat and from flat to none. Actually I want a control with flat border style but with custom color not in black. I do the following thing to achieve this control. Create a user control -> Add textbox control on it and move into the code.
My user control Paint Event is transform into the following code
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
ControlPaint.DrawBorder(e.Graphics, base.ClientRectangle, this.BackColor, ButtonBorderStyle.Solid);
}
My user control size changed event is transform into the following code
private void UserControl1_SizeChanged(object sender, EventArgs e)
{
if (!txtBox.Multiline)
{
txtBox.Width = this.Width - 2;
this.Height = txtBox.Height + 2;
return;
}
else
{
txtBox.Width = this.Width - 2;
txtBox.Height = this.Height - 2;
}
My textbox size changed event is transform into the following code
private void txtBox_SizeChanged(object sender, EventArgs e)
{
this.Height = txtBox.Height + 2;
this.Width = txtBox.Width + 2;
}
My user control font changed event is transform into the following code
private void UserControl1_FontChanged(object sender, EventArgs e)
{
txtBox.Font = this.Font;
}
My textbox font changed event is transform into the following code
private void txtBox_FontChanged(object sender, EventArgs e)
{
this.Height = txtBox.Height + 2;
this.Width = txtBox.Width + 2;
}
Add the following codes in your control class
private bool multiline = false;
public bool Multiline
{
get { return multiline; }
set
{
multiline = value;
txtBox.Multiline = multiline;
}
}
Set the X and Y location of the textbox to 1,1. Build the solution file and use your textbox control with custom color border feature.