c# - TextBox的OnPaint方法没有被调用?

标签 c# .net winforms textbox onpaint

我已经使用下面的代码创建了一个文本框,但是在文本框的任何情况下都不会触发 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);
    }
}

最佳答案

默认情况下,不会在 TextBox 上调用 OnPaint,除非您通过调用将其注册为自绘画控件:

SetStyle(ControlStyles.UserPaint, true);

例如来自您的 MyTextBox 构造函数。

关于c# - TextBox的OnPaint方法没有被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37944323/

相关文章:

javascript - 从动态表中获取每个单元格的值

c# - [A]MySql.Data.MySqlClient.MySqlConnection 无法转换为 [B]MySql.Data.MySqlClient.MySqlConnection

c# - 如何在 RDLC 字段中显示本地系统日期格式

c# - Convert.ToDouble 在法语系统中抛出错误

c# - myVariable(int) == 0 与 myVariable(Guid) == Guid.Empty 相同

c# - 如何在 C++ 中触发 C# COM 事件?

.net - 知道 Windows 用户是否处于事件状态

c# - 由于 Intellisense 错误而无法发布

c# - Telerik Winforms 主题标题栏不显示

c# - 如何在 MVP 被动 View 中显示复杂数据