Hi,@OmkarHcl. You could use the following code to accomplish it.
Codebedhind:
doubleunderlinetextbox tb = new doubleunderlinetextbox();
tb.Location = new Point(51, 257);
tb.Size = new Size(212, 100);
tb.Text = "hello";
this.Controls.Add(tb);
doubleunderlinetextbox:
public class doubleunderlinetextbox : TextBox
{
public doubleunderlinetextbox()
{
AutoSize = false;
BorderStyle = BorderStyle.None;
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Rectangle borderRect = new Rectangle(0, 0, Width, Height);
using (Pen topBorderPen = new Pen(BorderColor, 2))
{
e.Graphics.DrawLine(topBorderPen, borderRect.Left, borderRect.Top + 1, borderRect.Right, borderRect.Top + 1);
}
using (Pen underlineBorderPen = new Pen(BorderColor, 2))
{
e.Graphics.DrawLine(underlineBorderPen, borderRect.Left, borderRect.Bottom - 6, borderRect.Right, borderRect.Bottom - 6);
}
using (Pen underlineBorderPen = new Pen(BorderColor, 6))
{
e.Graphics.DrawLine(underlineBorderPen, borderRect.Left, borderRect.Bottom, borderRect.Right, borderRect.Bottom);
}
if (!string.IsNullOrEmpty(Text))
{
TextRenderer.DrawText(e.Graphics, Text, Font, borderRect, ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter);
}
}
private Color borderColor = Color.Black;
public Color BorderColor
{
get { return borderColor; }
set
{
borderColor = value;
Invalidate();
}
}
}
The result:
Update: I updated the code as follows, it will block part of the top line when entering text.
public class doubleunderlinetextbox : TextBox
{
private Rectangle outerRect;
private Rectangle innerRect;
public doubleunderlinetextbox()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
AutoSize = false;
Multiline = true;
int padding = 8;
outerRect = new Rectangle(0, 0, Width, Height);
innerRect = new Rectangle(outerRect.Left + padding, outerRect.Top + padding, outerRect.Width - padding * 2, outerRect.Height - padding * 3);
TextChanged += CustomTextBox_TextChanged;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (Pen topBorderPen = new Pen(BorderColor, 4))
{
e.Graphics.DrawLine(topBorderPen, outerRect.Left, outerRect.Top, outerRect.Right, outerRect.Top);
}
using (Pen underlineBorderPen = new Pen(BorderColor, 2))
{
e.Graphics.DrawLine(underlineBorderPen, outerRect.Left, outerRect.Bottom-10, outerRect.Right, outerRect.Bottom-10);
}
using (Pen underlineBorderPen = new Pen(BorderColor, 4))
{
e.Graphics.DrawLine(underlineBorderPen, outerRect.Left, outerRect.Bottom -6 , outerRect.Right, outerRect.Bottom -6);
}
using (Pen innerPen = new Pen(InnerBorderColor, 0))
{
e.Graphics.DrawRectangle(innerPen, innerRect);
}
using (SolidBrush textBrush = new SolidBrush(ForeColor))
{
StringFormat format = new StringFormat();
e.Graphics.DrawString(Text, Font, textBrush, innerRect, format);
}
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
int padding = 8;
outerRect = new Rectangle(0, 0, Width, Height);
innerRect = new Rectangle(outerRect.Left + padding, outerRect.Top + padding, outerRect.Width - padding * 2, outerRect.Height - padding * 2);
}
private Color borderColor = Color.Black;
public Color BorderColor
{
get { return borderColor; }
set
{
borderColor = value;
Invalidate();
}
}
private Color innerBorderColor = Color.Transparent;
public Color InnerBorderColor
{
get { return innerBorderColor; }
set
{
innerBorderColor = value;
Invalidate();
}
}
protected override void WndProc(ref Message m)
{
const int WM_CHAR = 0x0102;
const int WM_PASTE = 0x0302;
const int WM_CLEAR = 0x0303;
const int WM_CUT = 0x0300;
const int WM_KEYDOWN = 0x0100;
switch (m.Msg)
{
case WM_CHAR:
case WM_PASTE:
case WM_CLEAR:
case WM_CUT:
if (!innerRect.Contains(PointToClient(Cursor.Position)))
return;
break;
case WM_KEYDOWN:
if (((Keys)m.WParam) == Keys.Left || ((Keys)m.WParam) == Keys.Right)
return;
break;
}
base.WndProc(ref m);
}
private void CustomTextBox_TextChanged(object sender, System.EventArgs e)
{
// Restrict text within the inner rectangle
if (!innerRect.Contains(PointToClient(Cursor.Position)))
{
if (SelectionStart <= innerRect.Left)
{
SelectionStart = innerRect.Left ;
}
else if (SelectionStart >= innerRect.Right)
SelectionStart = innerRect.Right - 1;
Text = Text.Substring(0, innerRect.Left) + Text.Substring(innerRect.Right);
SelectionStart = innerRect.Left;
}
}
}
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.