How do you set a DateTimePicker to be read only?

47,580

Solution 1

You could hook the Changed event, and set the value back to your desired value (if different) -- this way you'll cover any cause for change (via mouse, or keyboard)

Have you considered using a different control, such as a read only textbox or even a label control?

Solution 2

I know this is very old but to help anyone else searching for this (As it was the first one I found through google) You can use:

this.dateTimePicker1.Enabled = false;

to make it act the same way as a a textbox would with this.textbox1.ReadOnly = true

Solution 3

This question--after six years--still seems to get some interest so I'll throw in my 2 cents: What works for me is 1) Make a UserControl and change the base class to DateTimePicker 2) Take a little bitmap snapshot of the control whenever the value changes 3) Intercept the WM_PAINT message and if our control is disabled draw the bitmap instead of the control. (Note: AutoScaleMode property in designer.cs makes compile error so just remove)

public partial class DateTimePickerWithReadOnly : DateTimePicker
{
  Bitmap ReadOnlyImage;
  // We maintain a "shadow" control to avoid capturing selections in the snapshot.
  // If you use different formatting or styles just make sure the shadow is set to match!
  DateTimePicker Shadow = new DateTimePicker(); 
  public DateTimePickerWithReadOnly()
  {
    InitializeComponent(); 
    CaptureBitmap();
    this.ValueChanged += new EventHandler(DateTimePickerWithReadOnly_ValueChanged);
  }
  private void CaptureBitmap()
  {
    Shadow.Value = Value;
    Shadow.Size = Size;
    ReadOnlyImage = new Bitmap(Width, Height);
    Shadow.DrawToBitmap(ReadOnlyImage, new Rectangle(0, 0, Size.Width, Size.Height));
  }
  void DateTimePickerWithReadOnly_ValueChanged(object sender, EventArgs e)
  {
    CaptureBitmap();
  }
  protected override void DefWndProc(ref Message m)
  {
    base.DefWndProc(ref m);
    // WM_PAINT is 0x000F
    if ((m.Msg == 0x000F) && !Enabled)
    {
      Graphics g = base.CreateGraphics();
      g.DrawImage(ReadOnlyImage, new Rectangle(0, 0, Size.Width, Size.Height));
      g.Dispose();
    }
  }
}

Solution 4

As another suggestion: Use a textbox (or a label) and make it read only. Display the value you want in it.

If you have to have a datetimepicker, create both a datetimepicker and a textbox. Lay them on top of each other. When you need the datetimepicker, hid the textbox. Etc.

Solution 5

on Keydown: e.SuppressKeyPress = true; worked for me, can still pick a date, but can't enter manually

Share:
47,580
cjk
Author by

cjk

People pay me to do things I like.

Updated on May 07, 2021

Comments

  • cjk
    cjk about 3 years

    I have a DateTimePicker (nullable version) that I need to be read only. I'm not happy with the display if it is disabled, so wanted to know if anyone had a nifty example of how to stop updates on the field?

    Thanks.

  • Vilius Surblys
    Vilius Surblys about 15 years
    You'd also need to disable keyboard input that would change the value
  • Nick Bedford
    Nick Bedford over 13 years
    DateTime.Now is not the only value you want to be able to use.
  • Guillermo Gutiérrez
    Guillermo Gutiérrez almost 11 years
    Can you point me which property is that, please? I thought I can't change the disabled style in winforms, but if it's possible, I would like to. Many thanks.
  • Sudhir Jonathan
    Sudhir Jonathan almost 11 years
    Sorry... it's been over four years now and I can't remember a thing about this answer or what I was talking about :(
  • Broots Waymb
    Broots Waymb about 8 years
    Simple solution that I totally overlooked (as usual). Worked for my case. The only way this solution wouldn't be all that desirable, is if you didn't like the greyed out look which you don't get with ReadOnly = true.
  • Robin Bennett
    Robin Bennett over 2 years
    Enabled is not the same as read only. When a text box is read only you can still click into it, move the caret around, select the text and copy it.
  • Robin Bennett
    Robin Bennett over 2 years
    Enabled is not the same as read only. When a text box is read only you can still click into it, move the caret around, select the text and copy it.
  • Robin Bennett
    Robin Bennett over 2 years
    This results in a box that appears to be editable but the value changes back to the old value when you leave the date picker.