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

DateTimePicker control with custom custom date format

4 Answers 639 Views
Calendar, DateTimePicker, TimePicker and Clock
This is a migrated thread and some comments may be shown as answers.
John Paul
Top achievements
Rank 1
John Paul asked on 04 Oct 2010, 11:41 AM
Hi,

We are currently implementing a control that lets a user specify a date range within our application.  This seems like an ideal use of two DateTimePicker controls (start and end date).

The problem is that we want the textbox element of this control to display the datetime in a way that does not seem to be supported by the general custom date format characters.

The company uses a format of YYYYWW (week-week) to represent it's dates and that's the preferred format any date input.

Is there any way to tell the text element to call one of our methods to convert the date from the calender control rather than the standard DateTime.ToString("custom format") method.

Thanks
JP

4 Answers, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 04 Oct 2010, 10:27 PM
Hello John,

I would suggest creating a custom RadDateTimePicker and create an  an FormatProvider event, that would handle the value changed event of the control, in order to change the display text based on one of your methods of date conversion.

using System.Windows.Forms;
using Telerik.WinControls.UI;
 
namespace TestDateTimePickerCustomFormat
{
    using System;
    using System.Drawing;
 
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form1_Load);
            this.Size = new Size(200, 60);
        }
 
        void Form1_Load(object sender, EventArgs e)
        {
            var customDateTimePicker1 = new CustomDateTimePicker();
            customDateTimePicker1.Dock = DockStyle.Fill;
 
            this.Controls.Add(customDateTimePicker1);
            customDateTimePicker1.Format = DateTimePickerFormat.Custom;
            customDateTimePicker1.FormatProvider += radDateTimePicker1_FormatProvider;
            customDateTimePicker1.Value = DateTime.Now;
        }
 
        string radDateTimePicker1_FormatProvider(object sender, CustomFormatProviderEventArgs args)
        {
            // get the required string value here
            return args.CurrentDate.ToShortDateString();
        }
    }
 
    public class CustomDateTimePicker : RadDateTimePicker
    {
        public delegate string CustomFormatProviderEventHandler(object sender, CustomFormatProviderEventArgs date);
 
        public event CustomFormatProviderEventHandler FormatProvider;
 
        protected override void OnValueChanged(EventArgs e)
        {
            base.OnValueChanged(e);
 
            if (this.Format == DateTimePickerFormat.Custom && this.FormatProvider != null)
            {
                this.DateTimePickerElement.TextBoxElement.Text = this.FormatProvider(this, new CustomFormatProviderEventArgs(this.Value));
            }
        }
    }
 
    public class CustomFormatProviderEventArgs : EventArgs
    {
        public DateTime CurrentDate { get; set; }
 
        public CustomFormatProviderEventArgs()
        {
        }
 
        public CustomFormatProviderEventArgs(DateTime dateTime)
        {
            this.CurrentDate = dateTime;
        }
    }
}

Hope this will help, please let me know if there is anything else i can help you with.

Best Regards,
Emanuel Varga
0
John Paul
Top achievements
Rank 1
answered on 05 Oct 2010, 09:31 AM
That worked brilliantly... and seemed obvious in hindsight.

Thanks for your help.
JP

0
Santosh
Top achievements
Rank 1
answered on 09 Oct 2012, 09:18 AM
I am using the raddatetimepicker version "2012.2.726.40" in my project.

My Requirement: I need weekly format to be displayed in RadDateTimePicker control, Example:"8 Oct, 2012 - 14 Oct 2012", the user shoudl be able to change the weeks.

I tried using the example but it is not working, can you please provide me an example ASAP.

Thank you,
0
Peter
Telerik team
answered on 10 Oct 2012, 03:47 PM
Hi Santosh,

This functionality is not supported, because RadDateTimePicker uses the DateTime structure which internally treats days and months as one value. Due to the nature of your request, I am not able to provide you with a proper solution.

Greetings,
Peter
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
Tags
Calendar, DateTimePicker, TimePicker and Clock
Asked by
John Paul
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
John Paul
Top achievements
Rank 1
Santosh
Top achievements
Rank 1
Peter
Telerik team
Share this question
or