Developing for Modern Windows

Tips, tricks, and guides for developing on modern Windows platforms

Use a Single Handler for Multiple Buttons

This quick tip is for beginners, and works for any version of Windows modern apps (though the code might differ slightly in previous versions – this code is specifically for Windows 10 UWP apps). In my early projects I found that I had some messy code with lots of button handlers for the many buttons on my page.

If I had a dozen buttons on my page I would have a dozen small methods that handled each of those button presses, such as:


private void MenuBtn_Settings_Tapped(object sender, TappedRoutedEventArgs e)
{
    // open the settings menu
}

Once you have a lot of buttons in your app it gets messy very quickly, and you often end up repeating code for buttons with similar functionality.

One day it dawned on me that I didn’t need a different handler for each button. The handler parameters contain a reference to the object that triggered the handler (the sender), and you can therefore determine what button was pressed by getting the sender’s name. Then you can simply decide what logic to follow based on what button was pressed.

Wiring up your buttons

Create a single handler method with a suitably generic name (and leave it empty for now):


private void MenuButton_Tapped(object sender, TappedRoutedEventArgs e)
{

    // TODO: write the code

}

Now for every button that you want to handle with this method, assign MenuButton_Tapped to its Tapped event:

Now when that button is pressed/tapped, the MenuButton_Tapped method will run.

Which button was pressed?

Since we are executing the same handler method for multiple buttons we need to determine which button was actually pressed. This is as simple as getting the button’s name. Let’s expand the method to the following:


private void MenuButton_Tapped(object sender, TappedRoutedEventArgs e)
{

  var button = sender as Button;
  var btnName = button.Name;
  
  // now you have the name of the button pressed in the btnName variable and can use that info to decide what to do
  if(btnName == "Settings_Button")
        Frame.Navigate(typeof(SettingsPage));

}

And there you have a single method that can act for several buttons.  The above code first casts the sender object to the type Button (we know it’s a Button because we are going to assign this handler to buttons) and reference is in the variable button. Then get the button’s name and store it in btnName as a string.

Next  I use a switch statement to decide what my app does for each button:


switch (btnName)
{
    case "Social_Button":
        Frame.Navigate(typeof(SocialPage));
        break;
    case "Settings_Button":
        Frame.Navigate(typeof(SettingsPage));
        break;
    default:
        break;
}

Make it even simpler

If your buttons all have the same type of functionality (such as selecting a colour) you can simply give the buttons meaningful names and do something like this:


player.FavouriteColour = btnName; // e.g. buttons called "Green", "Red", etc.

That’s all there is to it. This is much more efficient than having lots of separate functions, and it prevents confusion and drudging through your code to find the right event handler. You can also add more buttons very easily.

Tags: ,

Leave a Reply

Your email address will not be published.