This is a migrated thread and some comments may be shown as answers.

RadDateTimePicker sets time to midnight

7 Answers 197 Views
Calendar, DateTimePicker, TimePicker and Clock
This is a migrated thread and some comments may be shown as answers.
Chet Musialowski
Top achievements
Rank 1
Chet Musialowski asked on 09 Oct 2012, 02:19 PM
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

7 Answers, 1 is accepted

Sort by
0
Peter
Telerik team
answered on 10 Oct 2012, 03:48 PM
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 >>
0
Chet Musialowski
Top achievements
Rank 1
answered on 10 Oct 2012, 06:25 PM
Thank you !
0
Jayalaxmi
Top achievements
Rank 1
answered on 18 Jul 2016, 10:42 AM

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

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 18 Jul 2016, 11:27 AM
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.
0
Nick
Top achievements
Rank 1
answered on 10 Jun 2019, 05:52 PM

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)

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 11 Jun 2019, 10:15 AM
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.
0
Nick
Top achievements
Rank 1
answered on 11 Jun 2019, 11:21 AM

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
Tags
Calendar, DateTimePicker, TimePicker and Clock
Asked by
Chet Musialowski
Top achievements
Rank 1
Answers by
Peter
Telerik team
Chet Musialowski
Top achievements
Rank 1
Jayalaxmi
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Nick
Top achievements
Rank 1
Share this question
or