Get the text value of the button that was clicked in C#

Get the text value of the button that was clicked in C#

To get the text value of the button that was clicked in C#, you can use the sender parameter of the event handler to cast the sender object to a Button and then access the Content property of the button. Here's an example:

private void Button_Click(object sender, RoutedEventArgs e) { Button button = sender as Button; if (button != null) { string buttonText = button.Content.ToString(); // Do something with the button text } } 

In this example, the Button_Click event handler is called when a button is clicked. The sender parameter represents the object that raised the event, which in this case is the button that was clicked. The code casts the sender parameter to a Button object using the as operator and checks if it's not null before accessing its Content property. Finally, the code converts the Content value to a string using the ToString() method and stores it in a variable for further use.

Examples

  1. "C# get text value of clicked button in WinForms"

    private void Button_Click(object sender, EventArgs e) { Button clickedButton = (Button)sender; string buttonText = clickedButton.Text; Console.WriteLine($"Clicked Button Text: {buttonText}"); } 

    Description: Retrieves the text value of the clicked button in a WinForms application.

  2. "C# WPF get content of clicked button"

    private void Button_Click(object sender, RoutedEventArgs e) { Button clickedButton = (Button)sender; string buttonText = clickedButton.Content.ToString(); Console.WriteLine($"Clicked Button Content: {buttonText}"); } 

    Description: Retrieves the content (text) of the clicked button in a WPF application.

  3. "C# ASP.NET get text of clicked button in code-behind"

    protected void Button_Click(object sender, EventArgs e) { Button clickedButton = (Button)sender; string buttonText = clickedButton.Text; Response.Write($"Clicked Button Text: {buttonText}"); } 

    Description: Retrieves the text value of the clicked button in an ASP.NET code-behind file.

  4. "C# get text of dynamically created button"

    Button dynamicButton = new Button(); dynamicButton.Text = "Click me"; dynamicButton.Click += DynamicButton_Click; private void DynamicButton_Click(object sender, EventArgs e) { Button clickedButton = (Button)sender; string buttonText = clickedButton.Text; Console.WriteLine($"Clicked Button Text: {buttonText}"); } 

    Description: Creates a dynamic button and retrieves its text value when clicked.

  5. "C# Windows Forms get text of pressed ToolStripButton"

    private void ToolStripButton_Click(object sender, EventArgs e) { ToolStripButton clickedButton = (ToolStripButton)sender; string buttonText = clickedButton.Text; Console.WriteLine($"Clicked ToolStripButton Text: {buttonText}"); } 

    Description: Retrieves the text value of a clicked ToolStripButton in a Windows Forms application.

  6. "C# get text of clicked LinkButton in ASP.NET"

    protected void LinkButton_Click(object sender, EventArgs e) { LinkButton clickedButton = (LinkButton)sender; string buttonText = clickedButton.Text; Response.Write($"Clicked LinkButton Text: {buttonText}"); } 

    Description: Retrieves the text value of the clicked LinkButton in an ASP.NET application.

  7. "C# WPF get text value of clicked HyperlinkButton"

    private void HyperlinkButton_Click(object sender, RoutedEventArgs e) { HyperlinkButton clickedButton = (HyperlinkButton)sender; string buttonText = clickedButton.Content.ToString(); Console.WriteLine($"Clicked HyperlinkButton Text: {buttonText}"); } 

    Description: Retrieves the text value of the clicked HyperlinkButton in a WPF application.

  8. "C# get text value of pressed RadioButton"

    private void RadioButton_Click(object sender, RoutedEventArgs e) { RadioButton clickedButton = (RadioButton)sender; string buttonText = clickedButton.Content.ToString(); Console.WriteLine($"Clicked RadioButton Text: {buttonText}"); } 

    Description: Retrieves the text value of a clicked RadioButton in a WPF application.

  9. "C# WinForms get text of clicked CheckBox"

    private void CheckBox_Click(object sender, EventArgs e) { CheckBox clickedCheckBox = (CheckBox)sender; string checkBoxText = clickedCheckBox.Text; Console.WriteLine($"Clicked CheckBox Text: {checkBoxText}"); } 

    Description: Retrieves the text value of the clicked CheckBox in a WinForms application.

  10. "C# ASP.NET get text of pressed ImageButton"

    protected void ImageButton_Click(object sender, ImageClickEventArgs e) { ImageButton clickedButton = (ImageButton)sender; // Assuming the text is stored in the "AlternateText" property string buttonText = clickedButton.AlternateText; Response.Write($"Clicked ImageButton Text: {buttonText}"); } 

    Description: Retrieves the text value of the pressed ImageButton in an ASP.NET application.


More Tags

built-in interpolation grails-orm safari react-dnd core-text sharepoint confidence-interval undefined-reference vue-router

More C# Questions

More Chemistry Calculators

More Biochemistry Calculators

More Gardening and crops Calculators

More Chemical reactions Calculators