I have been using the Windows DateTimePicker to display a time only and allow the user to spin the time.
The key is to just have a line like ctrl.CustomFormat = "HH:mm". The only problem with this is that I have to have a time present in the control at startup. I'd prefer to have it blank until the user starts hitting the up/down buttons.
Can I use the UltraDateTimeEditor for this purpose? I would think so, I have already started the process. I am using a FormatString of hh:mm and a MaskInput of {time}. The Value, I leave blank. I have set the spin button style to "Always". All picked on designer. However, I have a few minor issues:
1. Can I get rid of the dropdown button? I don't need it.
2. The spinning doesn't work quite like the DateTimePicker. When you spin to 12 (on the hours for example) it stops rather than cyle to 1. Is there a way to duplicate the behaviro
3. With the Value not set, it displays as blank. That's fine (and exactly what I want!), but when you hit the spin button, only the hours part comes up. Then you have to do hours minutes and am/pm. It would be nicer if the first time the spin button was hit, it went straight to some time (like 08:00 AM). Can I do this?
4. When you're not editing the control, it doesn't display the AM or PM. I assume the formatstring can be modified to show this?
Thanks,
Andrew
1. Set DropDownButtonDisplayStyle to 'Never'
2. Set SpinWrap to true
3. Not intrinsically; the only solution I can think of would be to maintain an external variable that tracks whether the button has ever been clicked.
4. You used the uppercase "H", which denotes 24-hour time; if you use the lowercase "h", it will display as 12-hour time, and then you use the "t" format specifier to make the AM/PM appear. See here for MSDN documentation on format specifiers.
Brian,
Thanks for all the help. I think I'm very close right now. For #3, I did something slightly different. I hooked into the control Enter event and have:
private void dtpCustom6_Enter(object sender, EventArgs e)
{
if (dtpCustom6.Value == null)
DateTime dt1 = new DateTime(2000, 1, 1, 8, 30, 0);
dtpCustom6.Value = dt1;
}
The only improvement here is that I'd (possibly) like to set the value when the up/down spinner if first clicked. What's the event for that? I tried EditorSpinButtonClick, but that doesn't seem to be the one.
Also, I set SpinWrap to true. It behave slightly differently than the Windows control, which is a problem because users are accustomed to that. Withe the DateTimePicker, if you spin through the hours and get to 11 a.m. and then 12, it will change the AM to PM. The infragistics one should do this too. I'm guessing there's an easy way. As a last resort, if you supply the event that is fired when the spin buttons are clicked, I guess I could hook into that and see if it's the hours that are spinning and take action appropriately. Is there a spin event and does it say if the hours (versus minutes, etc) are spinning?
We are in the testing phase now of our application development. This particular control still seems problematic. Specifically, if the control is blank (null value), and the user clicks the up arrow, the hours section (not minutes, not seconds) increments by 1. How can we get the seconds to increment by 1?
You suggested in your last post to set SpinButtonDisplayStyle to Never and add a EditorSpinButton. I'm not sure I understood the part about how you decide which section (hours, minutes, seconds) you are in. What is the SelectionStart property? What does the 'length of each section' mean? Is this the previous value for each section?
More generally, do you have any samples of using the UltraDateTimeEditor with null values where one is only interested in HH:MM:SS ?
You might want to consider setting SpinButtonDisplayStyle to Never, and adding an EditorSpinButton; then you would be able to receive a notification when the spin buttons are clicked:
SpinEditorButton spinButton = new SpinEditorButton( "spin" );spinButton.SpinButtonType = SpinButtonTypes.NextOrPreviousItem;this.ultraDateTimeEditor.ButtonsRight.Add( spinButton );
this.ultraDateTimeEditor.EditorSpinButtonClick += new SpinButtonClickEventHandler(ultraNumericEditor1_EditorSpinButtonClick);
void ultraNumericEditor1_EditorSpinButtonClick(object sender, SpinButtonClickEventArgs e){ UltraDateTimeEditor dte = sender as UltraDateTimeEditor; EditorWithMask editor = dte.Editor as EditorWithMask;}
Note that the EditorWithMask class exposes a 'Sections' collection, which gives you each section of the mask; you can iterate this and get a reference to each constituent part of the mask, i.e., the MonthSection, DaySection, HourSection, etc. I don't know of a way to know which section the caret is currently in, so you might have to so something hacky like compare the SelectionStart property agains the length of each section. Once you know where you are, you can increment/decrement the appropriate component of the date.