繁体   English   中英

为NumericUpDown绘制边框

[英]Draw border for NumericUpDown

我在应用程序中有一个用户表单。 一些字段已经过验证。 如果字段的值错误,则为该控件绘制红色边框。 它是通过处理此控件的Paint事件而制成的。 我扩展了TextFieldDateTimePicker以从这些类对象获取Paint事件。 我对NumericUpDown类有问题。 它确实触发了Paint事件,但是调用了

ControlPaint.DrawBorder(e.Graphics, eClipRectangle, Color.Red, ButtonBorderStyle.Solid);

完全不执行任何操作。 有什么想法或建议吗? 如果找不到任何方法,则将添加一个面板来容纳NumericUpDown控件,然后将其背景色更改。

每次处理程序挂接到Paint事件时,我都会调用control.Invalidate()以重新绘制它。

尝试这个:

public class NumericUpDownEx : NumericUpDown
{
    bool isValid = true;
    int[] validValues = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        if (!isValid)
        {
            ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.Red, ButtonBorderStyle.Solid);
        }
    }

    protected override void OnValueChanged(System.EventArgs e)
    {
        base.OnValueChanged(e);

        isValid = validValues.Contains((int)this.Value);
        this.Invalidate();
    }
}

假设您输入的是int而不是十进制。 您的有效性检查可能会有所不同,但这对我有用。 如果新值不在定义的有效值中,它将在整个NumbericUpDown周围绘制红色边框。

诀窍是确保调用base.OnPaint 之后进行边框绘制。 否则,边框将被绘制。 从NumericUpDown继承而不是分配其Paint事件可能更好,因为重写OnPaint方法可以完全控制绘画顺序。

暂无
暂无

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

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