locked
How to set DateTimePicker (WinForms) control calendar automaticly on Today date RRS feed

  • Pergunta

  • Hello,
    I have WinForms application with custom control based on DateTimePicker.
    In OnDropDown event I want to open calendar with automaticly selected today date in it.
    I do not want to set Value property, just to open calendar on specific date.
    How do I do that?

    Thank you
    Tanya
    terça-feira, 22 de setembro de 2009 15:21

Todas as Respostas

  • Hello,

    The nullable datetime picker from CodeProject might help you to get there: http://www.codeproject.com/KB/selection/Nullable_DateTimePicker.aspx

    LS
    terça-feira, 22 de setembro de 2009 15:59
  • DTP already defaults to the current date.  Not sure why that wouldn't work for you.  You can override it if necessary by implementing the DropDown event:

            private void dateTimePicker1_DropDown(object sender, EventArgs e) {
                dateTimePicker1.Value = DateTime.Today;
            }


    Hans Passant.
    terça-feira, 22 de setembro de 2009 17:04
  • Tanya, I don't think there's any managed way to have the DateTimePicker show different date in the edit section and the dropdown section. So basically when you open the drop down menu, it will always default to the date selected in the edit section. You could try messing with the windows api to see if there's some way to make the date time picker change its mind but I wouldn't really recommend that... or you can somehow override the edit section, for example by doing custom painting or overlapping the text editor with some other control.
    terça-feira, 22 de setembro de 2009 19:03
  • Hello,
    I have WinForms application with custom control based on DateTimePicker.
    In OnDropDown event I want to open calendar with automaticly selected today date in it.
    I do not want to set Value property, just to open calendar on specific date.
    How do I do that?

    Thank you
    Tanya

    The .NET default DateTimePicker already highlights Today when we drop down the DataTimePicker to show the Calendar. If you see a different behavior of DateTimePicker. Could you please let us know how you embed the DateTimePicker in your custom control in a more detailed way. If you can simplify a project or some codes to share on the community, then we can do future investigation on the issue and give future suggestions.

    Otherwise, I think setting the DateTimePicker's value is the simplest way to achieve your objective. Why not?

    Look forward to you reply! Have a nice day.

    Best regards,
    Ji Zhou
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    quarta-feira, 23 de setembro de 2009 00:28
  • Hello Ji Zhou,
    Thank you for such a quick reply.
    I use following sample to display blank date: http://www.dotnetcurry.com/ShowArticle.aspx?ID=43
    If date is blank, the Value = 1/1/1800 and control's logic relies on it.
    But when calendar is opened, 1/1/1800 date is selected in it and this is very uncomfortable for the users.
    If I change the Value to Today in OnDropDown event, it looks like user already made his choice, but he did not yet, he just opened the calendar and may cancel the selection.
    This is the reason why I cannot change Value in OnDropDown event and I need another solution.
    Appreciate your help.
    Tanya.
    quarta-feira, 23 de setembro de 2009 12:32
  • Hello Ji Zhou,
    Thank you for such a quick reply.
    I use following sample to display blank date: http://www.dotnetcurry.com/ShowArticle.aspx?ID=43
    If date is blank, the Value = 1/1/1800 and control's logic relies on it.
    But when calendar is opened, 1/1/1800 date is selected in it and this is very uncomfortable for the users.
    If I change the Value to Today in OnDropDown event, it looks like user already made his choice, but he did not yet, he just opened the calendar and may cancel the selection.
    This is the reason why I cannot change Value in OnDropDown event and I need another solution.
    Appreciate your help.
    Tanya.
    quarta-feira, 23 de setembro de 2009 12:32
  • I think Leonardo is right.

     

    If you override the DateTimePicker and you leave the base.Value as it's default which is TODAY, (this.Value you will manage and store it in your private mValue field), then the DateTimePicker text when dropdown is closed is set from your Value but when it opens it will open according to the base.Value.

    Now you will have to take this code:

     

     /// <summary>

            /// Used to change the UDTP.Value on Closeup(Without this code Closeup only changes the base.Value)

            /// </summary>

            /// <param name="e"></param>

            protected override void WndProc(ref Message m)

            {

                if (m.Msg == 0x4e)

                {

                    NMHDR nm = (NMHDR)m.GetLParam(typeof(NMHDR));

                    if (nm.Code == -746 || nm.Code == -722)

                        this.Value = base.Value;//propagate change form base to UTDP

                }

                base.WndProc(ref m);

            }

     

     

    and change it to something like this

     

     /// <summary>

            /// Used to change the UDTP.Value on Closeup(Without this code Closeup only changes the base.Value)

            /// </summary>

            /// <param name="e"></param>

            protected override void WndProc(ref Message m)

            {

                if (m.Msg == 0x4e)

                {

                    NMHDR nm = (NMHDR)m.GetLParam(typeof(NMHDR));

                    if (nm.Code == -746 || nm.Code == -722)

                    {

                        this.Value = base.Value;//propagate change form base to UTDP

                        base.Value=DatTime.Today;//this line was added - we may need to add code to avoid the OnValueChanged raising the event

                     }

                }

                base.WndProc(ref m);

            }

     

     

    That should do what you want.

    segunda-feira, 18 de outubro de 2010 14:55
  • Hi,

    Did you ever manage to get this to work?  I have a similar issue with having a certain value (01/01/1980) making the date display as blank, but I want the DropDown to show the current date by default.

    Pete

    terça-feira, 3 de março de 2020 13:42