locked
Disable datepicker dropdown RRS feed

  • Question

  • Hi,

    I've been googling for some time, but nothing useful found.

    I have two DateTimePicker controls on my form. When value in first one is entered, value in the other one is automatic populated. I want to prevent user for manually input value to the second DateTimePicker, but I still want to keep the control looks enabled. So I want to disable DropDown.

    Does anyone have any suggestions?

    Thank you in advance.
    • Moved by CoolDadTx Monday, October 4, 2010 1:50 PM Winforms related (From:Visual C# General)
    Monday, October 4, 2010 11:23 AM

Answers

  • Hello Katmandu80,

    What kind of solution are you expecting for?

    If you want the value in the second datetimepicker automatically populate, why not use eg. a TextBox to show the value with setting its ReadOnly property to true?  It's not recommended to make things more complicated, or is there other requirement on the second datetimepicker makes you insist on showing value in DataTimePicker control other than TextBox control eg.?

    **********Anyway, for question of disabling  the DateTimePicker calendar dropping down, you may have a try with following code: 

    using System.ComponentModel;

    ...

    private bool allowDropDown = true;

    // Property to either allow a click on the dropdown to show the calendar or not.
    [DefaultValue(true)]
    public bool AllowDropDown
    {
     get { return allowDropDown; }
     set { allowDropDown = value; }
    }

    protected override void WndProc(ref System.Windows.Forms.Message m)
    {

     // If preventing the drop-down calender, then monitor Windows messages sent by the control,
     // looking for left mouse button click.

     const int WM_LBUTTONDOWN = 0x201;

     if (!(this.AllowDropDown) && m.Msg == WM_LBUTTONDOWN)
       {

       // The mouse coordinates relative to the control are in the LParam argument of the message.
       // Extract the packed coordinates from the 32-bit integer.
       int x = LoWord(m.LParam.ToInt32);
       int y = HiWord(m.LParam.ToInt32);

       // Test to see if the mouse click was within the drop-down arrow's region.
       // If the current mouse's X position is < 20 pixels, then don't send the message.
       if (Math.Abs(x - this.Width) > 20)
         base.WndProc(m);

     } else
       base.WndProc(m);
     
    }

    private int LoWord(int value)
    {
     return Convert.ToInt16(new Point(value).X);
    }

    private int HiWord(int value)
    {
     return Convert.ToInt16(new Point(value).Y);
    }

    Sincerely,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Monday, October 11, 2010 2:39 AM
    Thursday, October 7, 2010 8:38 AM

All replies

  • Hi,

    Please refer the below link , It will be useful,

    http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/be1e7b66-75ab-46e6-bf72-f47e685d58aa

    Regards,

    S.Subashselvan.

    Monday, October 4, 2010 11:47 AM
  • Thank you Subashselvan,

    I have already gone through that post before asking  question. I thought if there is some other solution.

     

    Regards,

    A.Danilovic

    Monday, October 4, 2010 12:13 PM
  • Hello Katmandu80,

    What kind of solution are you expecting for?

    If you want the value in the second datetimepicker automatically populate, why not use eg. a TextBox to show the value with setting its ReadOnly property to true?  It's not recommended to make things more complicated, or is there other requirement on the second datetimepicker makes you insist on showing value in DataTimePicker control other than TextBox control eg.?

    **********Anyway, for question of disabling  the DateTimePicker calendar dropping down, you may have a try with following code: 

    using System.ComponentModel;

    ...

    private bool allowDropDown = true;

    // Property to either allow a click on the dropdown to show the calendar or not.
    [DefaultValue(true)]
    public bool AllowDropDown
    {
     get { return allowDropDown; }
     set { allowDropDown = value; }
    }

    protected override void WndProc(ref System.Windows.Forms.Message m)
    {

     // If preventing the drop-down calender, then monitor Windows messages sent by the control,
     // looking for left mouse button click.

     const int WM_LBUTTONDOWN = 0x201;

     if (!(this.AllowDropDown) && m.Msg == WM_LBUTTONDOWN)
       {

       // The mouse coordinates relative to the control are in the LParam argument of the message.
       // Extract the packed coordinates from the 32-bit integer.
       int x = LoWord(m.LParam.ToInt32);
       int y = HiWord(m.LParam.ToInt32);

       // Test to see if the mouse click was within the drop-down arrow's region.
       // If the current mouse's X position is < 20 pixels, then don't send the message.
       if (Math.Abs(x - this.Width) > 20)
         base.WndProc(m);

     } else
       base.WndProc(m);
     
    }

    private int LoWord(int value)
    {
     return Convert.ToInt16(new Point(value).X);
    }

    private int HiWord(int value)
    {
     return Convert.ToInt16(new Point(value).Y);
    }

    Sincerely,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Monday, October 11, 2010 2:39 AM
    Thursday, October 7, 2010 8:38 AM
  • Hi Helen,

    Thank you for you suggestion. I will probably do it on an easier way, setting value to a text box.

    Best regards,

    A.Danilovic


    Best regards, Ana Danilovic
    Thursday, October 7, 2010 12:05 PM