How to don't let DatePicker be grey after IsEnabled is set to false? RRS feed

  • Question

  • When I set DatePicker IsEnabled as false, font color of DatePicker change to grey. All DatePicker is grey. I tried to set foreground to black, opacity to 1, but nothings helped. So how to don't let DatePicker to be grey? 

    Best way would be to set IsReadonly to true, but DatePicker doesn't have this function/ method.

    Thank you for answer.


    • Edited by TakeshiKitano Friday, April 24, 2020 10:21 AM
    • Moved by CoolDadTx Friday, April 24, 2020 2:24 PM Winforms related
    Friday, April 24, 2020 10:18 AM

All replies

  • The behavior is correct. Disabled controls are rendered in a "disabled" style. Why wouldn't you want this? If the control is using the "enabled" style then the user might try to use it and wonder why it doesn't work.

    As for how to change it that is a good question because it is backed by Windows so you're limited to what Windows allows. I think you're only option is to set the Enabled to false and then explicitly change the various format properties and set them back to the enable states. You can get the currently configured system colors (such as disabled) via SystemColors.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, April 24, 2020 2:24 PM
  • Maybe you can hide the picker and show a read-only textbox instead. Make the picker visible again when you want to edit the date.

    Saturday, April 25, 2020 5:33 AM
  • Hello,

    This is against how Windows handles enable/disables controls. it breaks continuity of how other applications present disabled controls. 

    To get what you want a custom DateTimePicker is needed, I found one on Stackoverflow which visually appears differently in how it's drawn but does in a sense what you are asking.

    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Windows.Forms.VisualStyles;
    namespace ChangeToYourNamespace
        public class DateTimePickerSpecial : DateTimePicker
            public DateTimePickerSpecial()
                SetStyle(ControlStyles.UserPaint, true);
            protected override void OnPaint(PaintEventArgs e)
                Graphics g = CreateGraphics();
                Rectangle dropDownRectangle = new Rectangle(
                    ClientRectangle.Width - 20, 0, 20, 20);
                Brush bkgBrush;
                ComboBoxState visualState;
                if (Enabled)
                    bkgBrush = new SolidBrush(BackColor);
                    visualState = ComboBoxState.Normal;
                    bkgBrush = new SolidBrush(BackColor);
                    visualState = ComboBoxState.Disabled;
                    bkgBrush, 0, 0, ClientRectangle.Width, ClientRectangle.Height);
                g.DrawString(Text, Font, Brushes.Black, 0, 2);
                ComboBoxRenderer.DrawDropDownButton(g, dropDownRectangle, visualState);
            [Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
            public override Color BackColor
                get => base.BackColor;
                set => base.BackColor = value;

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Saturday, April 25, 2020 10:13 AM
  • I find better way ...

    when I need I use this

    private void datePicker1_PreviewTextInput(object sender, TextCompositionEventArgs e)
        if ( ... )
            e.Handled = true;

    Tuesday, June 9, 2020 12:49 PM