Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8559649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:06:08+00:00 2026-06-11T16:06:08+00:00

I have a lot of buttons on a WPF window. To avoid coding the

  • 0

I have a lot of buttons on a WPF window. To avoid coding the same click event over and over again I would like them to all trigger the same method and take the appropriate action based on which button I call.

I know I can cast the sender to a button and then check the name and route the code from there, but then I have to have all the names hard coded, which could be a mess if buttons get renamed a some stage.

What is a better way of doing this? Can I for example name the button from an enum? Or is it possible to get the button names for the check by actually referencing the buttons directly?

Something like this maybe?

private void btnAddTerminatorElement_Click(object sender, RoutedEventArgs e)
    {
        MsoAutoShapeType shapeType;

        switch (((Button)sender).Name)
        {
            case this.btnAddTerminatorElement.Name:
                shapeType = MsoAutoShapeType.msoShapeFlowchartTerminator;
                break;

        }

        CreateChartElement(this.targetWorksheet.Shapes.AddShape(MsoAutoShapeType.msoShapeFlowchartTerminator, 100, 100, 50, 50));
    }

But this doesn’t work as is because a constant is needed for the comparison…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:06:09+00:00Added an answer on June 11, 2026 at 4:06 pm

    The only good way I have come up with so far is not to use a switch statement, but to go for separate if blocks in the event function to check the caller name. Something like this:

    private void btnAddTerminatorElement_Click(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;
            MsoAutoShapeType shapeType = MsoAutoShapeType.msoShapeRectangle;
    
            if (sender == this.btnAddTerminatorElement)
                shapeType = MsoAutoShapeType.msoShapeFlowchartTerminator;
    
            if (sender == this.btnAddDecisionElement)
                shapeType = MsoAutoShapeType.msoShapeFlowchartDecision;
    
            CreateChartElement(this.targetWorksheet.Shapes.AddShape(shapeType, 100, 100, 50, 50));
        }
    

    The only method I can think of so far that is robust enough to cope with renaming the buttons. If there are any alternatives I’d be most interested in hearing about them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a lot of buttons that i would like to give a title
I have a lot of buttons in my ViewController. I would like to have
I have a lot of buttons on main activity. Each button run this same
I'm doing this interface where I have a lot of buttons that are just
In my application I have a lot of UI elements (particuarly buttons) where I
I have an android application. There are a lot of buttons in it but
I have lot of code in my Tornado app which looks like this: @tornado.web.asynchronous
My WPF application is becoming a real pain. I have created a Window and
In my scenario I have a lot of buttons or other controls which I
I have a lot on .meta-entity containers. I add Buttons with .after() $(.entry-utility).after(<div class='show_meta'>Artikel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.