简体   繁体   English

TextBox OnPaint 方法没有被调用?

[英]TextBox OnPaint method is not called?

I have use the following code to create a textbox, but the paint method is not triggered at any situation of the textbox.我已经使用以下代码创建了一个文本框,但是在文本框的任何情况下都不会触发绘制方法。 Can you suggest a solution to trigger the OnPaint() ?你能建议一个触发 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 will not be called on a TextBox by default unless you register it as a self-painting control, by making a call to :默认情况下,不会在 TextBox 上调用 OnPaint,除非您将其注册为自绘控件,方法是调用:

SetStyle(ControlStyles.UserPaint, true);

You need to switch the calls in your OnPaint<\/code>您需要在OnPaint<\/code>中切换调用

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    ControlPaint.DrawBorder(e.Graphics, this.Bounds, Color.Red, ButtonBorderStyle.Solid);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM