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
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
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
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
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)
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
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