Michael
Michael

Reputation: 749

C# Winforms DateTimePicker Custom Formatted to Tenths of a Second / Default Selection to Seconds

My application currently uses the DateTimePicker custom formatted to display HH:mm:ss.

How can I format it so to allow for tenths of a second? My second question is that when I tab into the datetimepicker, the Hours section is selected first by default, is there anyway to change this so that seconds are selected first by default? I noticed that If you click the updown arrows without tabbing into the control the seconds section is selected by default.

Any experts on the datetimepicker out there? Or does anyone have an ideas on an alternative I can use to implement these features?

Below is a picture of how it is formatted as well as the properties: DateTimePickers

Properties

Upvotes: 0

Views: 2447

Answers (2)

Loathing
Loathing

Reputation: 5291

You could try to force the DateTimePicker to display milliseconds, but it will be a painful experience. You are better off using a MaskedTextBox. You can download TimePicker.cs from sourceforge: https://sourceforge.net/projects/time-picker/

enter image description here

Here is some sample code:

    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Form f = new Form();
        var dp = new TimePicker("hh:mm:ss.f", true) { Location = new Point(100, 100) };
        dp.ByValue[2] = false;
        f.Controls.Add(dp);
        var bt1 = new Button { Text = "Now", Location = new Point(110, 130) };
        bt1.Click += delegate {
            dp.Value = DateTime.Now;
        };

        int k = 0;
        dp.ValueChanged += delegate {
            bt1.Text = "Now " + (k++);
        };
        f.Controls.Add(bt1);
        Application.Run(f);
    }

Upvotes: 2

Charlie
Charlie

Reputation: 2271

This could work

  1. Add a DomainUpDown control

  2. Add a bunch of datetimes that vary by .1 seconds and display them as strings formatted to show miliseconds. Your user can then click up and down to see .1 second deltas

        string dateString = "7/9/2015 9:32:45.126 AM";
        DateTime dateValue = DateTime.Parse(dateString);
    
        for (int i = 0; i < 5; i++)
        {
            dtPicker.Items.Add(dateValue.AddSeconds(.1*i).ToString("MM/dd/yyyy hh:mm:ss.fff tt"));
        }
    
  3. If you want to let the user pick say the hour first and then generate your datetime deltas from there it won't work as well. You could create multiple numeric controls, but at that point you might want to go down the path of extending the datetime picker like here.

Upvotes: 0

Related Questions