user1791240
user1791240

Reputation: 142

VirtualKey C# Windows Store App

I'm writing a Windows Store App for Windows 8. I have two buttons on the screen that I'd like to toggle with the space bar: when the start button in pressed I disable it and enable the stop button, and visa versa.

I can start and stop using the spacebar, but I have a problem after I click any button with the mouse: then the focus is on a different button, which I then accidentally press with the space bar.

I want the focus to be on the stop button after I clicked the start button, so I can press the stop button with the space bar. How should I do that?

public void Grid_KeyDown(object sender, KeyRoutedEventArgs e)
{
   if (e.Key == VirtualKey.Space)
   {
       if(!isPlaying)
       {
           StartButton_Click(sender, null);
       }
       else
       {
           StopButton_Click(sender, null);
       }
   }
}    

Regards

Upvotes: 2

Views: 266

Answers (1)

Jim O'Neil
Jim O'Neil

Reputation: 23754

In StartButton_Click call

StopButton.Focus(Focus.Programmatic);

Upvotes: 1

Related Questions