Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why DateTimePicker BackColor Disable manual typing?

I override OnPaint method in the aim to put color in the textbox of a DateTimePicker Control and it disable the manual typing in the textbox ?

have you some ideas to solve this problem ?

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; }
    }

}

I give more details concerning "Manual typing" : It's when you press tab and you go on the DateTimePicker. Then you can enter a new date using your keyboard.

Like that :

enter image description here

like image 613
Julien698 Avatar asked Oct 18 '22 23:10

Julien698


1 Answers

Keyboard input was not disabled, highlighting capability was, because of your simplistic OnPaint implementation. Initially we have:

enter image description here

And then clicking the control to gain focus and typing, let's say, "07/04/1776" (IMPORTANT: backslashes included), we get:

enter image description here

and finally, selecting the drop down button, just to confirm:

enter image description here

This is the code:

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; }
    }
}

The Form contains a regular DateTimePicker and a BCDateTimePicker, with the green background (set through VS Designer).

So, it works as expected. The text box even updates dynamically as the date is being typed.

EDIT 1: This GIF was too large to upload on SO:

See animated GIF here

EDIT 2: Note about ControlStyles.UserPaint - MSDN

If true, the control paints itself rather than the operating system doing so. If false, the Paint event is not raised. This style only applies to classes derived from Control.

Note that BCDateTimePicker lost its textbox edit-highlighting capability. That's because your implementation of OnPaint is way more simplistic than what is done by the operating system. But keyboard input was not disabled and is still operational.

like image 137
jsanalytics Avatar answered Oct 21 '22 15:10

jsanalytics