3 Replies - 3677 Views - Last Post: 03 May 2011 - 04:24 AM Rate Topic: -----

#1 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

change behavior of ENTER key to TAB for a user control

Posted 02 May 2011 - 11:25 AM

Hey =) I've got a user control laid out (it's in wpf actually) but I need for the enter key to tab to the next user control on the window.

On my actual window, I have several user controls, so this is a pretty sticky situation. It seems I can't do send keys with user controls, nor can I use "GetNextControl().Focus();" apparently. What should I do?

Here's some relevant code (notice the last switch case... empty=( ):


        private void TimeIn1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.A:
                    comboBox1.SelectedIndex = 0;
                    e.Handled = true;
                    //send tab
                    break;
                case Key.P:
                    comboBox1.SelectedIndex = 1;
                    e.Handled = true;
                    // send tab
                    break;
                case Key.OemPeriod:
                    int cursorHere = ((TextBox)sender).Selectionstart;
                    string allTheText = ((TextBox)sender).Text;
                    string insertion = allTheText.Substring(0, cursorHere) + ":" + allTheText.Substring(cursorHere);
                    ((TextBox)sender).Text = insertion;

                    ((TextBox)sender).Selectionstart = cursorHere + 1;
                    
                    e.Handled = true;
                    break;
                case Key.Enter:
                    // change to next tab item...
                    e.Handled = true;

                    break;
            }
        }




Is This A Good Question/Topic? 0
  • +

Replies To: change behavior of ENTER key to TAB for a user control

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: change behavior of ENTER key to TAB for a user control

Posted 02 May 2011 - 11:44 AM

this.SelectNextControl(this.ActiveControl, true, true, true, true);


http://msdn.microsof...extcontrol.aspx
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4498
  • View blog
  • Posts: 7,850
  • Joined: 08-June 10

Re: change behavior of ENTER key to TAB for a user control

Posted 02 May 2011 - 11:49 AM

That works for Windows Forms, but I believe this is a WPF question. I'll move this over to the WPF forum.
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: change behavior of ENTER key to TAB for a user control

Posted 03 May 2011 - 04:24 AM

This should work for you...

UIElement element = sender as UIElement;

if (element != null)
{
    element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}



http://msdn.microsof....movefocus.aspx
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1