日期:2014-05-17  浏览次数:20947 次

提问:如何将CSharp中的TextBox将边框改变成底部一条线
提问:如何将CSharp中的TextBox将边框改变成底部一条线

------解决方案--------------------
参考方法http://blog.csdn.net/yanchao1023/article/details/5458803
------解决方案--------------------
重绘textbox
 public partial class posTexbox : System .Windows .Forms .TextBox 
{
public posTexbox()
{
InitializeComponent();
this.Width = 100;
this.BorderStyle = BorderStyle.None;
}
private Color _linecolor = Color.Red;
/// <summary>
/// 线条颜色
/// </summary>
public Color LineColor
{
get
{
return this._linecolor;
}
set
{
this._linecolor = value;
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
//this.BackColor = Color.DarkSlateBlue;
base.OnPaint(e);
}
private const int WM_PAINT = 0xF;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_PAINT)
{
DrawLine();
}
}
private void DrawLine()
{
Graphics g = this.CreateGraphics();
using (Pen p = new Pen(this._linecolor))
{
g.DrawLine(p, 0, this.Height - 1, this.Width, this.Height - 1);
}
}

protected override void OnEnter(EventArgs e)
{
base.OnEnter(e);
this.SelectAll();
this.BackColor = Color.White;
this.ForeColor = Color.Black;

}
protected override void OnLeave(EventArgs e)
{
base.OnLeave(e);
this.BackColor = Color.Black;
this.ForeColor = Color.White;
}

}