这可能是一个令人尴尬的问题,但我使用 MSDN 网站的帮助创建了一个带有红色边框的自定义文本框。
我知道我从 UserControl 类继承并覆盖了它的 OnPaint() 方法。
有人可以告诉我在哪里调用 OnPaint() 方法,因为我在代码中看不到它。
这是代码 -
namespace CustomTextBox
{
public partial class BorderTextBox : UserControl
{
private TextBox textBox;
private Color myColor = Color.Red;
public BorderTextBox()
{
InitializeComponent();
this.textBox = new TextBox();
this.DoubleBuffered = true;
this.Padding = new Padding(2);
this.textBox.BorderStyle = BorderStyle.None;
this.textBox.Dock = DockStyle.Fill;
this.Controls.Add(this.textBox);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Pen borderPen = new Pen(myColor);
e.Graphics.DrawRectangle(borderPen,
new Rectangle(0, 0, this.ClientSize.Width - 1, this.ClientSize.Height - 1));
}
}
}