You must Sign In to post a response.
  • Category: WPF

    How to create the Time Picker User control ?

    Hai Everyone,

    i want use Time control only in window Application . Like HH:MM:AM .
    can anyone give some ideas how to create the Timer User Control in WPF ?
  • #768824
    Do not go for any third party control just you need to use DateTimePicker that enables users to choose a time only
    see below snippet

    timePicker = new DateTimePicker();
    timePicker.Format = DateTimePickerFormat.Time;
    timePicker.ShowUpDown = true

    If you want to do it from properties, you can do this by setting the Format property of DateTimePicker to DateTimePickerFormat.Time and ShowUpDown property to true. Also, customFormat can be set in properties.

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #768825
    Hai Prasad ,

    Thanks for Your reply .

    But WPF don't have DateTimePicker Control .
    please give suggestion .

    i have attached sample image . what i need .

    Delete Attachment

  • #768862
    You can use this code snippet as example to create the Time Picker User control ?
     
    public ucDateTimeUpDown()
    {
    InitializeComponent();
    this.DataContext = this;
    AmPmTypes.Add("AM");
    AmPmTypes.Add("PM");
    CurrentTime = DateTime.UtcNow.ToLocalTime();
    SelectedTime = CurrentTime.ToLocalTime().ToString("t");
    }

    #endregion

    #region Public Properties

    public ObservableCollection<string> AmPmTypes
    {
    get { return _amPmTypes; }
    set { _amPmTypes = value; }
    }
    public string DisplayTime
    {
    get { return _currentTime.ToLocalTime().ToString("t"); }
    }

    public string DisplayAmPm
    {
    get
    {
    if (_currentTime.ToLocalTime().Hour >= 0
    && _currentTime.ToLocalTime().Hour < 12)
    _displayAmPm = AmPmTypes.FirstOrDefault(s => s.Equals("AM"));
    else
    {
    if (_currentTime.ToLocalTime().Hour >= 12)
    {
    _displayAmPm = AmPmTypes.FirstOrDefault(s => s.Equals("PM"));
    }
    }

    return _displayAmPm;
    }
    set
    {
    if (!value.Equals(_displayAmPm))
    {
    if (value.Equals("PM"))
    CurrentTime = CurrentTime.ToLocalTime().AddHours(12);
    else
    {
    CurrentTime = CurrentTime.ToLocalTime().AddHours(-12);
    }
    }
    _displayAmPm = value;
    }
    }

    public string DisplayTimeHours
    {
    get
    {
    var hours = _currentTime.ToLocalTime().Hour;
    return hours > 12 ? (hours - 12).ToString("00") : hours.ToString("00");
    }
    set
    {
    var hour = 0;
    Int32.TryParse(value, out hour);
    CurrentTime = CurrentTime.ToLocalTime().AddHours(hour);
    OnPropertyChanged("DisplayTime");
    OnPropertyChanged("DisplayTimeHours");
    OnPropertyChanged("DisplayTimeMinutes");
    }
    }

    public string DisplayTimeMinutes
    {
    get { return _currentTime.ToLocalTime().Minute.ToString("00"); }
    set
    {
    var minutes = 0;
    Int32.TryParse(value, out minutes);
    CurrentTime = CurrentTime.ToLocalTime().AddMinutes(minutes);
    OnPropertyChanged("DisplayTime");
    OnPropertyChanged("DisplayTimeHours");
    OnPropertyChanged("DisplayTimeMinutes");
    }
    }
    public DateTime CurrentTime
    {
    get { return _currentTime; }
    set
    {
    _currentTime = value;

    OnPropertyChanged("CurrentTime");
    OnPropertyChanged("DisplayTime");
    OnPropertyChanged("DisplayTimeHours");
    OnPropertyChanged("DisplayTimeMinutes");
    OnPropertyChanged("DisplayAmPm");
    SelectedTime = value.ToLocalTime().ToString("t");
    }
    }

    #endregion

    #region Dependency Properties

    public static readonly DependencyProperty SelectedTimeProperty
    = DependencyProperty.Register(
    "SelectedTime", typeof (string), typeof (ucDateTimeUpDown),
    new PropertyMetadata(default(string)));

    public string SelectedTime
    {
    get { return ((DateTime) GetValue(SelectedTimeProperty))
    .ToLocalTime().ToString("t"); }
    set { SetValue(SelectedTimeProperty, value); }
    }

    #endregion

    #region Methods

    private void MinutesUpButton_OnClick(object sender, RoutedEventArgs e)
    {
    CurrentTime = CurrentTime.AddMinutes(1);
    SelectedTime = CurrentTime.ToLocalTime().ToString("t");
    }

    private void MinutesDownButton_OnClick(object sender, RoutedEventArgs e)
    {
    CurrentTime = CurrentTime.AddMinutes(-1);
    SelectedTime = CurrentTime.ToLocalTime().ToString("t");
    }

    private void HourUpButton_OnClick(object sender, RoutedEventArgs e)
    {
    CurrentTime = CurrentTime.AddHours(1);
    SelectedTime = CurrentTime.ToLocalTime().ToString("t");
    }
    private void HourDownButton_OnClick(object sender, RoutedEventArgs e)
    {
    CurrentTime = CurrentTime.AddHours(-1);
    SelectedTime = CurrentTime.ToLocalTime().ToString("t");
    }
    #endregion

  • #768870
    Hai HemzChand,
    The WPF doesn't have the default time-picker control as inbuilt. So either you need to extend the existing control or you can get it from extended toolkit.
    To create the new time-picker control by extending the existing one, you can use the below code:

    http://helpyourselfhere.blogspot.my/2015/03/wpf-simple-timepicker-user-control-for.html

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


  • Sign In to post your comments