Auto-selecting parts on LeftArrow or RightArrow keypress

Editors for WPF Forum

Posted 14 years ago by Geir Schjorlien
Version: 10.1.0523
Avatar
Hi,

I would like to accomplish the following in a TimeSpanEditBox (or in any "parts" editor):
    When one of the parts in the edit box is clicked, automatically select that part (similar to what a double-click does).
    When the LeftArrow or RightArrow key is pressed, move focus to the next or previous part and automatically select that part.
Can you please point me in the right direction?

Thanks,
Geir

Comments (6)

Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Geir,

You would probably need to handle the PreviewMouseDown event on the TimeSpanEditBox. Then look for an event coming from an instance of MaskedTextBox. When you encounter one, call SelectAll and set e.Handled to true.

Same for the Left and Right arrow keys, but you'd need to handle PreviewKeyDown.

You could probably wrap this up into an attached behavior, so that you could easily reuse it across all PartEditBox-derivations.


Actipro Software Support

Posted 14 years ago by Geir Schjorlien
Avatar
Hi,

I tried handling the PreviewMouseDown event, but this is fired only once with "sender=TimeSpanEditBox".

The code below of course doesn't work because "Keyboard.FocusedElement" has not been set to the correct MaskedTextBox yet, but would it be possible for me to hook in somewhere after the TimeSpanEditBox has set focus to the clicked part?

void TimeSpanEditBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
   IInputElement element = Keyboard.FocusedElement;
   if (element is MaskedTextBox)
   {
      ((MaskedTextBox)element).SelectAll();
      e.Handled = true;
   }
}
Thanks,
Geir
Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Geir,

If you attached the handler to the TimeSpanEditBox, then yes it would always be the sender. You would need to check the e.Source/e.OriginalSource for the MaskedTextBox, no the Keyboard.FocusedElement.


Actipro Software Support

Posted 14 years ago by Geir Schjorlien
Avatar
Hi,

I've gotten the Left and Right arrow keys to work as I would like using the following code:

void TimeSpanEditBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
   IInputElement element = Keyboard.FocusedElement;

   if (element is MaskedTextBox)
   {
      if (e.Key == Key.Right)
      {
         ((MaskedTextBox)element).MoveFocus(new TraversalRequest(FocusNavigationDirection.Right));
         element = Keyboard.FocusedElement;
         ((MaskedTextBox)element).SelectAll();
         e.Handled = true;
      }
      else if (e.Key == Key.Left)
      {
         ((MaskedTextBox)element).MoveFocus(new TraversalRequest(FocusNavigationDirection.Left));
         element = Keyboard.FocusedElement;
         ((MaskedTextBox)element).SelectAll();
         e.Handled = true;
      }
   }
}
However, I'm still struggling with the PreviewMouseDown event (and as you probably have understood, I'm a real newbie :)).
I'm handling the PreviewMouseDown event on the TimeSpanEditBox, and you mention above that I should check the e.Source/e.OriginalSource for the MaskedTextBox. However, I never get a MaskedTextBox in e.Source. It's either TimeSpanDaysPart, TimeSpanHoursPart etc. I discovered that these classes contain a MaskedTextBox property, but that doesn't help me either since it's a protected property, and I can't do something like this:

void TimeSpanEditBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
   if (e.Source is Part)
   {
      ((Part)e.Source).MaskedTextBox.SelectAll();
      e.Handled = true;
   }
}
Other hints are greatly appreciated :) (without having to write my own custom editor...)

Thanks,
Geir
Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Geir,

The PreviewMouseDown may be suppressed for the MaskedTextBox, since it's technically inside the control template of the associated Part.

Regardless, you could get the MaskedTextBox using VisualTreeHelperExtended.GetFirstDescendant(part, typeof(MaskedTextBox)). VisualTreeHelperExtended is located in our Shared library.


Actipro Software Support

Posted 14 years ago by Geir Schjorlien
Avatar
Hi,

Following code did the trick

void TimeSpanEditBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
   if (e.Source is Part)
   {
      MaskedTextBox mtb = (MaskedTextBox)VisualTreeHelperExtended.GetFirstDescendant((Part)e.Source, typeof(MaskedTextBox));
      if (mtb != null)
      {
         mtb.Focus();
         mtb.SelectAll();
         e.Handled = true;
      }
   }
}
Thanks for your help :)
Geir
The latest build of this product (v24.1.3) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.