1

I have 5 buttons that are going to call this popup function to send an email. How can I figure out which clicked button called the function?

   public void popup(object sender, EventArgs e)
    {

        if (MessageBox.Show("You may not Bind, Change, Or Alter Insurance Coverage through e-mail. Confirmation of this e-mail by us initiates any changes to your Insurance. If you need any immediate service please contact our office at 1-800-875-5720.", "IMPORTANT!", MessageBoxButtons.OKCancel) == DialogResult.OK)
        {

            {
                string email = "mailto:davidadfa.t@ifdafdadf.com";
                Process.Start(email);
            }
        }
    }
Gaffi
  • 4,307
  • 8
  • 43
  • 73
Snake3ite
  • 43
  • 2
  • 7

5 Answers5

5

The sender object on the event handler will be the button that was pressed

Button b = sender as Button;
Rob Rodi
  • 3,476
  • 20
  • 19
2

The sender object is the button that calls the method:

var buttonId = ((Button)sender).ID;
Joe
  • 6,401
  • 3
  • 28
  • 32
1

Sender in your event is Button object that has been clicked.

But if you need to know that maybe it would be better to slightly refactor your code and set separate button click event and move that functionality in another method and then call it from various click events and eventually send params

Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
1

try

public void popup(object sender, EventArgs e)
{
Button TheButtonClicked = sender as Button; // this gives access to the button 

    if (MessageBox.Show("You may not Bind, Change, Or Alter Insurance Coverage through e-mail. Confirmation of this e-mail by us initiates any changes to your Insurance. If you need any immediate service please contact our office at 1-800-875-5720.", "IMPORTANT!", MessageBoxButtons.OKCancel) == DialogResult.OK)
    {

        {
            string email = "mailto:davidadfa.t@ifdafdadf.com";
            Process.Start(email);
        }
    }
}

For reference see this.

Community
  • 1
  • 1
Yahia
  • 69,653
  • 9
  • 115
  • 144
  • +1 for the link with more detail, plus showing all the code to identify where your solution should go. – Gaffi Apr 06 '12 at 14:11
1

http://msdn.microsoft.com/en-us/library/3exstx90.aspx

  1. Select the control to which you want to connect an event handler.

  2. In the Properties window, click the Events button ().

  3. Click the name of the event that you want to handle.

  4. In the value section next to the event name, click the drop-down button to display a list of existing event handlers that match the method signature of the event you want to handle.

5 .Select the appropriate event handler from the list. Code will be added to the form to bind the event to the existing event handler.