DateTimePicker Pick Both Date and Time - Winform

9 posts, 2 answers
  1. Tim
    Tim avatar
    5 posts
    Member since:
    Jan 2020

    Posted 12 Feb Link to this post

    I found multiple requests back in 2009 for this feature and it appears it has been implemented.

    According to this page, the DateTimePicker does have the ability to show both dates and times to choose.

    https://www.telerik.com/products/winforms/datetimepicker.aspx

    The page is marked as UI for Winforms and I have been trying to find out how to make the DateTimePicker to show both dates and times.

    There is no code for it and no articles. This is for winforms.

    Is this even possible?

     

    Thanks.

  2. Answer
    Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3957 posts

    Posted 17 Feb Link to this post

    Hello, Tim,

    In order to show the time part in the drop down, it is necessary to set the ShowTimePicker property to true.

    Please refer to the following code snippet demonstrating how to setup RadDateTimePicker in order to show date and time part in the popup calendar:

                this.radDateTimePicker1.Format = DateTimePickerFormat.Custom;
                this.radDateTimePicker1.CustomFormat = "dd/MM/yyyy hh:mm";
                this.radDateTimePicker1.DateTimePickerElement.ShowTimePicker = true;
                this.radDateTimePicker1.DateTimePickerElement.CalendarSize = new Size(400, 300);

    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.
  3. Tim
    Tim avatar
    5 posts
    Member since:
    Jan 2020

    Posted 17 Feb in reply to Dess | Tech Support Engineer, Sr. Link to this post

    Thanks for the info, it worked.

    Is it possible to do the same with a GridViewDateTimeColumn?

  4. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3957 posts

    Posted 20 Feb Link to this post

    Hello, Tim,

    In the RadGridView.CellEditorInitialized event you have access to the editor. Thus, you can apply the same properties to the RadDateTimeEditorElement:
            private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
            {
                RadDateTimeEditor editor = e.ActiveEditor as RadDateTimeEditor;
                if (editor!=null)
                {
                    RadDateTimeEditorElement el = editor.EditorElement as RadDateTimeEditorElement;
    
                    el.Format = DateTimePickerFormat.Custom;
                    el.CustomFormat = "dd/MM/yyyy hh:mm";
                    el.ShowTimePicker = true;
                    el.CalendarSize = new Size(400, 300);
                }
            }
    Should you have further questions please let me know.

    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.
  5. Tim
    Tim avatar
    5 posts
    Member since:
    Jan 2020

    Posted 20 Feb in reply to Dess | Tech Support Engineer, Sr. Link to this post

    Thanks again. I did find this information and it works. Now for one more question.

    I'm trying to capture the Close button event on the calendar so it will update the database without leaving the cell.

    I can capture it on the CellEndEdit or CellValueChanged but the user will have to leave the cell.

    I found possibly a mouse_down event handler to add to the calendar but am having trouble adding it and finding the close button.

    Maybe you can point me into the right direction?

    Thanks again.

  6. Answer
    Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3957 posts

    Posted 24 Feb Link to this post

    Hello, Tim,

    You can subscribe to the RadDateTimePickerCalendar.TimePicker.CloseButtonClicked event in order to detect when the Close button is clicked:
            private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
            {
                RadDateTimeEditor editor = e.ActiveEditor as RadDateTimeEditor;
                if (editor != null)
                {
                    RadDateTimeEditorElement el = editor.EditorElement as RadDateTimeEditorElement;
    
                    el.Format = DateTimePickerFormat.Custom;
                    el.CustomFormat = "dd/MM/yyyy hh:mm";
                    el.ShowTimePicker = true;
                    el.CalendarSize = new Size(400, 300);
                    el.Calendar.ShowFooter = true;
                    RadDateTimePickerCalendar calendarBehavior = el.GetCurrentBehavior() as RadDateTimePickerCalendar;
                    calendarBehavior.TimePicker.CloseButtonClicked -= TimePicker_CloseButtonClicked;
                    calendarBehavior.TimePicker.CloseButtonClicked += TimePicker_CloseButtonClicked;
                }
            }
    
            private void TimePicker_CloseButtonClicked(object sender, EventArgs e)
            {
            }
    I hope this information helps.  

    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.
  7. Tim
    Tim avatar
    5 posts
    Member since:
    Jan 2020

    Posted 24 Feb in reply to Dess | Tech Support Engineer, Sr. Link to this post

    Thanks but the CloseButtonClicked  property is not available.

    I'm proficient in C# and actually prefer it but in this project we are using VB.

    Here's the code that does not have the CloseButtonClicked  property...

        Private Sub dgProduction_CellEditorInitialized(sender As Object, e As GridViewCellEventArgs) Handles dgProduction.CellEditorInitialized      
            Dim editor As RadDateTimeEditor = TryCast(e.ActiveEditor, RadDateTimeEditor)
            If editor IsNot Nothing Then
                Dim element As RadDateTimeEditorElement = TryCast(editor.EditorElement, RadDateTimeEditorElement)
                element.CalendarSize = New System.Drawing.Size(400, 300)
                element.ShowTimePicker = True
                element.TextBoxElement.EnableMouseWheel = False

                Dim calendarBehavior As RadDateTimePickerCalendar = TryCast(element.GetCurrentBehavior(), RadDateTimePickerCalendar)

                'No CloseButtonClicked available here
                'calendarBehavior.TimePicker.  

            End If
        End Sub

  8. Tim
    Tim avatar
    5 posts
    Member since:
    Jan 2020

    Posted 24 Feb in reply to Tim Link to this post

    Never mind, it wasn't in the intellisense but it works.

    Thanks!!!

  9. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3957 posts

    Posted 25 Feb Link to this post

    Hello, Tim

    Here is the relevant VB.NET code snippet:
        Private Sub RadGridView1_CellEditorInitialized(sender As Object, e As Telerik.WinControls.UI.GridViewCellEventArgs)
            Dim editor As RadDateTimeEditor = TryCast(e.ActiveEditor, RadDateTimeEditor)
    
            If editor IsNot Nothing Then
                Dim el As RadDateTimeEditorElement = TryCast(editor.EditorElement, RadDateTimeEditorElement)
                el.Format = DateTimePickerFormat.Custom
                el.CustomFormat = "dd/MM/yyyy hh:mm"
                el.ShowTimePicker = True
                el.CalendarSize = New Size(400, 300)
                el.Calendar.ShowFooter = True
                Dim calendarBehavior As RadDateTimePickerCalendar = TryCast(el.GetCurrentBehavior(), RadDateTimePickerCalendar)
                RemoveHandler calendarBehavior.TimePicker.CloseButtonClicked, AddressOf TimePicker_CloseButtonClicked
                AddHandler calendarBehavior.TimePicker.CloseButtonClicked, AddressOf TimePicker_CloseButtonClicked
            End If
        End Sub
    
        Private Sub TimePicker_CloseButtonClicked(sender As Object, e As EventArgs)
            Console.WriteLine("Close")
        End Sub
    I have also attached a sample VB project for your reference. 

    Should you have further questions please let me know.

    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.
Back to Top