我重写OnPaint方法的目的是将颜色放置在DateTimePicker控件的文本框中,并且禁用了在文本框中的手动键入?

您有解决这个问题的一些想法吗?

public class BCDateTimePicker : DateTimePicker
{
    public BCDateTimePicker()
    {
        this.SetStyle(ControlStyles.UserPaint, true);
    }

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        Graphics g = this.CreateGraphics();
        Rectangle dropDownRectangle = new Rectangle(ClientRectangle.Width - 20, 0, 20, 20);
        Brush bkgBrush;
        ComboBoxState visualState;
        if (this.Enabled)
        {
            bkgBrush = new SolidBrush(this.BackColor);
            visualState = ComboBoxState.Normal;
        }
        else
        {
            bkgBrush = new SolidBrush(this.BackColor);
            visualState = ComboBoxState.Disabled;
        }
        g.FillRectangle(bkgBrush, 0, 0, ClientRectangle.Width, ClientRectangle.Height);
        g.DrawString(this.Text, this.Font, Brushes.Black, 0, 2);
        ComboBoxRenderer.DrawDropDownButton(g, dropDownRectangle, visualState);

        g.Dispose();
        bkgBrush.Dispose();
    }

    [Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public override Color BackColor
    {
        get { return base.BackColor; }
        set { base.BackColor = value; }
    }

}

我会提供有关“手动输入”的更多详细信息:
这是当您按Tab键并继续使用DateTimePicker时。然后,您可以使用键盘输入新日期。

像那样 :

c# - 为什么DateTimePicker BackColor禁用手动键入?-LMLPHP

最佳答案

由于您的OnPaint实现简单,因此并未禁用键盘输入,而是突出显示了功能。最初,我们有:

c# - 为什么DateTimePicker BackColor禁用手动键入?-LMLPHP

然后单击控件以获取焦点并键入,例如,“07/04/1776”(重要:包括反斜杠),我们得到:

c# - 为什么DateTimePicker BackColor禁用手动键入?-LMLPHP

最后,选择下拉按钮,只是为了确认:

c# - 为什么DateTimePicker BackColor禁用手动键入?-LMLPHP

这是代码:

public class BCDateTimePicker : DateTimePicker
{
    public BCDateTimePicker()
    {
        this.SetStyle(ControlStyles.UserPaint, true);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics g = this.CreateGraphics();

        Rectangle dropDownRectangle = new Rectangle(ClientRectangle.Width - 20, 0, 20, 20);
        Brush bkgBrush;
        ComboBoxState visualState;
        if (this.Enabled)
        {
            bkgBrush = new SolidBrush(this.BackColor);
            visualState = ComboBoxState.Normal;
        }
        else
        {
            bkgBrush = new SolidBrush(this.BackColor);
            visualState = ComboBoxState.Disabled;
        }
        g.FillRectangle(bkgBrush, 0, 0, ClientRectangle.Width, ClientRectangle.Height);
        g.DrawString(this.Text, this.Font, Brushes.Black, 0, 2);
        ComboBoxRenderer.DrawDropDownButton(g, dropDownRectangle, visualState);

        g.Dispose();
        bkgBrush.Dispose();
    }

    [Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public override Color BackColor
    {
        get { return base.BackColor; }
        set { base.BackColor = value; }
    }
}
Form包含常规的DateTimePickerBCDateTimePicker,带有绿色背景(通过VS Designer设置)。

因此,它按预期工作。文本框甚至会随着键入日期而动态更新。

编辑1 :该GIF太大,无法在SO上上传:

See animated GIF here

编辑2 :关于ControlStyles.UserPaint - MSDN的注意事项



请注意,BCDateTimePicker失去了其文本框的编辑突出显示功能。这是因为与操作系统相比,OnPaint的实现更加简单。但是并未禁用键盘输入,并且仍然可以操作。

关于c# - 为什么DateTimePicker BackColor禁用手动键入?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33436162/

10-12 14:51