---
title: "Handling multiple button actions in one action in C#"  
description: "Handling multiple button actions in one action in C#"  
author: "Manoj Bhatt"  
published: 2013-11-13  
updated: 2013-11-13  
canonical: https://www.mindstick.com/forum/1722/handling-multiple-button-actions-in-one-action-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Handling multiple button actions in one action in C#

I have 70 buttons whose names are like button1, button2, button3 and so on. My aim is that whenever button1 is clicked, it will say "1", button2 will [say as](https://answers.mindstick.com/qa/94627/what-does-section-406-say-as-per-the-ipc) "2" and so on for the others.

**The [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) for button1 to speak is:**

```
SpeechSynthesizer synthesizer = new SpeechSynthesizer();private void button1_Click(object sender, EventArgs e){   
synthesizer.Speak("1");}For button2private void button2_Click(object sender, EventArgs e){   
synthesizer.Speak("2");}
```

and so on for other 68 buttons.

[Now](https://yourviews.mindstick.com/view/81402/it-s-liberals-vs-progressives-in-us-politics-now) it is difficult to implement the 70 [button](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net)'s [actions](https://www.mindstick.com/forum/156060/what-are-the-actions-performed-by-sql-server). These button actions follow a [pattern](https://www.mindstick.com/forum/2314/what-is-the-mvp-and-mvc-pattern) - so can [anyone](https://www.mindstick.com/articles/23207/how-to-find-the-best-fit-job-for-anyone) suggest a more [efficient](https://www.mindstick.com/articles/33637/three-tips-for-an-energy-efficient-home-for-2019) way I can implement these button [handlers](https://www.mindstick.com/forum/1388/button-handlers-in-xaml) to save me [writing](https://www.mindstick.com/articles/12997/5-essential-tips-on-resume-writing-for-business-analysts) out 70 different actions?

## Replies

### Reply by Anonymous User

Hi Manoj,\

Use same handler for all buttons. Sender of event will be the button which raised event. You can get it's name and extract text to say:

```
private void button_Click(object sender, EventArgs e){    Button button = (Button)sender;    string text = button.Name.Substring("button".Length);    synthesizer.Speak(text);}
```


---

Original Source: https://www.mindstick.com/forum/1722/handling-multiple-button-actions-in-one-action-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
