RadDateTimePicker sets time to midnight

8 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. Chet Musialowski
    Chet Musialowski avatar
    19 posts
    Member since:
    Nov 2009

    Posted 10 Oct 2012 Link to this post

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

    Posted 18 Jul 2016 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

     

  5. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3224 posts

    Posted 18 Jul 2016 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.
  6. Nick
    Nick avatar
    6 posts
    Member since:
    Aug 2016

    Posted 10 Jun Link to this post

    Has anyone tried a VB version of this?

    Converting the C# code from Peter (10 Oct 2012), I get this for the code in the example - which gives the error " 'Public Event Opened As EventHandler' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event." on the two Load event lines calendarBehavior.PopupControl....:

        Private timeTest As DateTime

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim element As RadDateTimePickerElement = RadDateTimePicker1.DateTimePickerElement
            Dim calendarBehavior As RadDateTimePickerCalendar = TryCast(element.GetCurrentBehavior(), RadDateTimePickerCalendar)
            calendarBehavior.PopupControl.Opened = New EventHandler(PopupControl_Opened)
            calendarBehavior.PopupControl.Closed = New RadPopupClosedEventHandler(PopupControl_Closed)
        End Sub

        Private Sub PopupControl_Closed(ByVal sender As Object, ByVal args As RadPopupClosedEventArgs)
            Dim dateStart As DateTime = RadDateTimePicker1.Value.Date
            RadDateTimePicker1.Value = New DateTime(dateStart.Year, dateStart.Month, dateStart.Day, timeTest.Hour, timeTest.Minute, timeTest.Second)
        End Sub

        Private Sub PopupControl_Opened(ByVal sender As Object, ByVal e As EventArgs)
            Me.timeTest = Me.RadDateTimePicker1.Value
        End Sub


    (Ver: 2018.1.220.40)

  7. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3224 posts

    Posted 11 Jun Link to this post

    Hello, Nick,        

    Please refer to the following code snippet demonstrating how to convert the mentioned C# code to VB.NET:

    Private time As DateTime
     
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Dim element As RadDateTimePickerElement = Me.RadDateTimePicker1.DateTimePickerElement
        Dim calendarBehavior As RadDateTimePickerCalendar = TryCast(element.GetCurrentBehavior(), RadDateTimePickerCalendar)
        AddHandler    calendarBehavior.PopupControl.Opened ,AddressOf PopupControl_Opened
        AddHandler calendarBehavior.PopupControl.Closed,AddressOf PopupControl_Closed
    End Sub
     
    Private Sub PopupControl_Closed(ByVal sender As Object, ByVal args As RadPopupClosedEventArgs)
        Dim dateValue As DateTime = Me.radDateTimePicker1.Value.Date
        Me.RadDateTimePicker1.Value = New DateTime(dateValue.Year, dateValue.Month, dateValue.Day, Me.time.Hour, Me.time.Minute, Me.time.Second)
    End Sub
     
    Private Sub PopupControl_Opened(ByVal sender As Object, ByVal e As EventArgs)
        Me.time = Me.RadDateTimePicker1.Value
    End Sub
     
    I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

    Regards,
    Dess | Tech Support Engineer, Sr.
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  8. Nick
    Nick avatar
    6 posts
    Member since:
    Aug 2016

    Posted 11 Jun Link to this post

    Hello Dess.  Thanks for the AddHandler fix.  Perfect.

    I've also completed the modified RadDateTimePicker if anyone needs it:

    Imports Telerik.WinControls.UI
    Public Class ModifiedRadDateTimePicker
        Inherits RadDateTimePicker
     
        Private timeFix As DateTime
     
        Public Sub New()
            Dim element As RadDateTimePickerElement = Me.DateTimePickerElement
            Dim calendarBehavior As RadDateTimePickerCalendar = TryCast(element.GetCurrentBehavior(), RadDateTimePickerCalendar)
            AddHandler calendarBehavior.PopupControl.Opened, AddressOf PopupControl_Opened
            AddHandler calendarBehavior.PopupControl.Closed, AddressOf PopupControl_Closed
        End Sub
     
        Public Overrides Property ThemeClassName As String
            Get
                Return GetType(RadDateTimePicker).FullName
            End Get
            Set(value As String)
                MyBase.ThemeClassName = value
            End Set
        End Property
     
        Private Sub PopupControl_Closed(ByVal sender As Object, ByVal args As RadPopupClosedEventArgs)
            Dim dateFix As DateTime = Me.Value.Date
            Me.Value = New DateTime(dateFix.Year, dateFix.Month, dateFix.Day, Me.timeFix.Hour, Me.timeFix.Minute, Me.timeFix.Second)
        End Sub
     
        Private Sub PopupControl_Opened(ByVal sender As Object, ByVal e As EventArgs)
            Me.timeFix = Me.Value
        End Sub
     
    End Class
Back to Top