RadDateTimePicker sets time to midnight

5 posts, 0 answers
  1. Chet Musialowski
    Chet Musialowski avatar
    19 posts
    Member since:
    Nov 2009

    Posted 09 Oct 2012 Link to this post

    Hello,

    I am using the RadDateTimePicker and the RadTimePicker (WinForms Version 2012.2.608.20) to set the Date and Time of a meeting (they are bound to the same column of a datatable).

    I'm finding that when I change the date, the time is always being reset to midnight.

    Is there a way to get the RadDateTimePicker to only change the date, leaving the time alone ?

    Thanks,
    Chet
  2. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 10 Oct 2012 Link to this post

    Hi Chet,

    Thank you for contacting the Telerik support.

    RadDateTimePicker always resets the time when user select the new date from the calendar.
    You can avoid this behavior by handling the PopupOpened and PopupClose events:

    private DateTime time;
    private void Form1_Load(object sender, EventArgs e)
    {
        RadDateTimePickerElement element = this.radDateTimePicker1.DateTimePickerElement;
        RadDateTimePickerCalendar calendarBehavior = element.GetCurrentBehavior() as RadDateTimePickerCalendar;
        calendarBehavior.PopupControl.Opened += new EventHandler(PopupControl_Opened);
        calendarBehavior.PopupControl.Closed += new RadPopupClosedEventHandler(PopupControl_Closed);
    }
     
    void PopupControl_Closed(object sender, RadPopupClosedEventArgs args)
    {
        DateTime date = this.radDateTimePicker1.Value.Date;
        this.radDateTimePicker1.Value = new DateTime(date.Year, date.Month, date.Day, this.time.Hour, this.time.Minute, this.time.Second);
    }
     
    void PopupControl_Opened(object sender, EventArgs e)
    {
        this.time = this.radDateTimePicker1.Value;
    }

    Please refer to the sample project.

    All the best,
    Peter
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Chet Musialowski
    Chet Musialowski avatar
    19 posts
    Member since:
    Nov 2009

    Posted 10 Oct 2012 Link to this post

    Thank you !
  5. Jayalaxmi
    Jayalaxmi avatar
    1 posts
    Member since:
    Jul 2016

    Posted 18 Jul in reply to Peter Link to this post

    Hi Telerik Team,

    I am using the Telerik version of(2014.2.617.20) and I do have the same issue. In my form I have more than 10 raddatetimepickers

    for each control I have repeat the same code or is there any optimized way to do this.

    Any help would be appreciated.

    Thanks,

    Jayalaxmi

     

  6. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 18 Jul Link to this post

    Hello Jayalaxmi,

    Thank you for writing.
     

    You can create a derivative of RadDateTimePicker implementing the custom behavior in it. Then, use the custom control where it necessary:
    public class MyDateTimePicker : RadDateTimePicker
    {
        public override string ThemeClassName 
        {
            get
            {
                return typeof(RadDateTimePicker).FullName; 
            }
        }
         
        protected override void CreateChildItems(Telerik.WinControls.RadElement parent)
        {
            base.CreateChildItems(parent);
            RadDateTimePickerElement element = this.DateTimePickerElement;
            RadDateTimePickerCalendar calendarBehavior = element.GetCurrentBehavior() as RadDateTimePickerCalendar;
            calendarBehavior.PopupControl.Opened += new EventHandler(PopupControl_Opened);
            calendarBehavior.PopupControl.Closed += new RadPopupClosedEventHandler(PopupControl_Closed);
        }
     
        private DateTime time;
     
        void PopupControl_Closed(object sender, RadPopupClosedEventArgs args)
        {
            DateTime date = this.Value.Date;
            this.Value = new DateTime(date.Year, date.Month, date.Day, this.time.Hour, this.time.Minute, this.time.Second);
        }
     
        void PopupControl_Opened(object sender, EventArgs e)
        {
            this.time = this.Value;
        }
    }

    I hope this information helps. Should you have further questions I would be glad to help.

    Regards,
    Dess
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
Back to Top
UI for WinForms is Visual Studio 2017 Ready