繁体   English   中英

C#WInforms更改按钮单击事件时的边框样式

[英]C# WInforms Change border style on button click event

我正在尝试在按钮单击事件上更改文本框(txtUser)的边框颜色(类似于表单验证,如果输入为空,则调用将边框着色为红色的方法)。 我做了一些谷歌搜索,发现了这一点:

void myControl1_Paint(object sender, PaintEventArgs e)
{
ControlPaint.DrawBorder(e.Graphics, this.txtUser.ClientRectangle, Color.Black, ButtonBorderStyle.Solid);
}

但是我很难理解应该在何处或如何调用该方法,或者将(对象发送者,PaintEventArgs e)作为参数的方法。 任何解释表示赞赏。

您需要从TextBox继承,然后重写OnPaint方法。 这样的事情应该起作用:

public class ValidateEdit : TextBox
{
    bool _InError;

    public ValidateEdit()
    {
        SetStyle(ControlStyles.UserPaint, true);
    }

    public bool InError {
        get {
            return _InError;
        }
        set
        {
            _InError = value;
            Refresh();
        }
    }

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

暂无
暂无

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

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