问题描述
我使用以下代码创建了一个TextBox,但在TextBox的任何情况下都不会触发Paint方法。您能提出一个触发OnPaint()的解决方案吗?
public class MyTextBox : TextBox
{
protected override void OnPaintBackground(PaintEventArgs pevent)
{
base.OnPaintBackground(pevent);
}
protected override void OnPaint(PaintEventArgs e)
{
ControlPaint.DrawBorder(e.Graphics,this.Bounds, Color.Red,ButtonBorderStyle.Solid);
base.OnPaint(e);
}
protected override void OnTextChanged(EventArgs e)
{
this.Invalidate();
this.Refresh();
base.OnTextChanged(e);
}
}
推荐答案
默认情况下不会在文本框上调用OnPaint,除非您通过调用:
将其注册为自绘制控件SetStyle(ControlStyles.UserPaint, true);
例如从MyTextBox构造函数。