Problem with extending DateTimeEditBox and using the "MMM" custom format specifier

Editors for WPF Forum

Posted 6 years ago by Geir Schjorlien
Version: 12.2.0573
Platform: Silverlight Controls
Environment: Windows 7 (64-bit)
Avatar

Hi,

I have extended the DateTimeEditBox class with functionality for auto-selecting all the text when the user clicks a "Part" or uses the left and right arrow keys: 

public class MyDateTimeEditBox : DateTimeEditBox
{
	public MyDateTimeEditBox()
	{
		PreviewMouseDown += new MouseButtonEventHandler(MyDateTimeEditBox_PreviewMouseDown);
		PreviewKeyDown += new KeyEventHandler(MyDateTimeEditBox_PreviewKeyDown);
	}

	void MyDateTimeEditBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
	{
		Part part = e.Source as Part;
		if (part != null)
		{
			MaskedTextBox mtb = (MaskedTextBox)VisualTreeHelperExtended.GetFirstDescendant(part, typeof(MaskedTextBox));
			if (mtb != null)
			{
				mtb.Focus();
				mtb.SelectAll();
				e.Handled = true;
			}
		}
	}

	void MyDateTimeEditBox_PreviewKeyDown(object sender, KeyEventArgs e)
	{
		MaskedTextBox mtb = Keyboard.FocusedElement as MaskedTextBox;
		if (mtb != null)
		{
			if (e.Key == Key.Right)
			{
				mtb.MoveFocus(new TraversalRequest(FocusNavigationDirection.Right));
				mtb = Keyboard.FocusedElement as MaskedTextBox;
				mtb.SelectAll();
				e.Handled = true;
			}
			else if (e.Key == Key.Left)
			{
				mtb.MoveFocus(new TraversalRequest(FocusNavigationDirection.Left));
				mtb = Keyboard.FocusedElement as MaskedTextBox;
				mtb.SelectAll();
				e.Handled = true;
			}
		}
	}
}

This works fine except when I'm using the "MMM" custom format specifier. When I set Format="dd MMM yyyy HH:mm:ss", I'm not able to click the month-part, and when I use the left and right arrow-keys, the month-part is skipped. When tracing the code, I can see that it enters the "MyDateTimeEditBox_PreviewMouseDown" with the correct MaskedTextBox, but the month-part will not become selected.

If I remove the e.Handled=true in "MyDateTimeEditBox_PreviewMouseDown", I can click the month part, but the text is not selected.

Is this a "missing feature" or do I need to implement it in another way?

 

Thanks,

Geir

Comments (4)

Answer - Posted 6 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Geir,

In the case of MMM format, it ends up using an InlineComboBox instead of a MaskedTextBox for input of month names.  So if you change your code to handle that, perhaps it will work.


Actipro Software Support

Posted 6 years ago by Geir Schjorlien
Avatar

Hi,

Thank you for the quick reply - as always :).

I was able to find the InlineComboBox, but I'm not sure what to do with it to make the text selected. By "selected" I mean the same thing as what happens when you double-click the contents of a "Part".

The other issue was to make the left and right arrow keys work as Tab and Shift-Tab. I didn't have much luck with this either. Any idea why the "mtb.MoveFocus(new TraversalRequest(FocusNavigationDirection.Right/Left))" skips the month part when format is "MMM"? Tab and Shift-Tab works.

Thanks,

Geir

Answer - Posted 6 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Geir,

If you can put together a complete sample project that reproduces your issue and email it over to our support address, then we can take a closer look.

For the keyboard navigation, I believe you may want to use Next/Previous instead of Left/Right. I think Left/Right work between MaskedTextBox controls because they have the same visual parent. While InlineComboBox itself is not focusable, but it contains a nested MaskedTextBox. So it's possible that Left/Right do not work properly for nested controls, even if they are technically to the left/right of the current control. The Next/Previous sould work the same as the Tab/Shift+Tab keys.

[Modified 6 years ago]


Actipro Software Support

Posted 6 years ago by Geir Schjorlien
Avatar

Hi, 

I have sent you a small sample project now.

Keyboard navigation with Next/Previous worked perfectly. Can't believe I didn't think of that myself... :)

 

Thanks,

Geir

The latest build of this product (v2018.1 build 0675) was released 12 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.