Why does DateTimePicker BackColor Disable manual input?

Am I overriding the OnPaint method to put color in the text box of a DateTimePicker control and disable manual text input in the text box?

Do you have any idea 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 will talk more about "manual input": This is when you click on a tab and you continue to DateTimePicker. Then you can enter a new date using the keyboard.

Like:

enter image description here

+5
source share
1 answer

Keyboard input was not disabled, the highlight function was due to your simplified implementation of OnPaint . Initially, we have:

enter image description here

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

enter image description here

and finally by selecting the drop-down button 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; } } } 

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

So, it works as expected. The text box even updates dynamically as you enter the date.

EDIT 1 : this GIF was too big to load on SO:

Watch animated GIF here

EDIT 2 : ControlStyles.UserPaint Note - MSDN

If true , the control draws itself, and does not make the operating system. If false , the Paint event is not raised. This style applies only to classes derived from Control.

Note that BCDateTimePicker lost the ability to highlight a text field. This is because your OnPaint implementation is more streamlined than what the operating system does. But keyboard input has not been disabled and still works.

+3
source

Source: https://habr.com/ru/post/1234864/


All Articles