2012-04-06 34 views
1

我有5个按钮将调用此弹出功能发送电子邮件。我怎样才能找出哪个点击按钮称为功能?多个按钮调用相同的函数。可以找出哪个按钮称为函数?

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:[email protected]"; 
       Process.Start(email); 
      } 
     } 
    } 

回答

5

在事件处理sender对象将被按下

Button b = sender as Button; 
+0

System.Web.UI.WebControls.Button或System.Windows.Forms.Button – Snake3ite 2012-04-06 13:57:16

+0

@ Snake3ite是WPF还是WinForms? – Yahia 2012-04-06 13:58:21

+0

System.Web.UI.WebControls.Button用于Web应用程序; System.Windows.Forms.Button用于WinForms应用程序,System.Windows.Controls.Button用于WPF应用程序。 – Joe 2012-04-06 14:06:17

1

发件人在事件的按钮已被点击的按钮对象。

但是,如果你需要知道,也许这将是更好的稍微重构代码,并设置单独的按钮单击事件,而在另一个方法移动该功能,然后从不同的单击事件调用它,并最终发送PARAMS

1

尝试

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:[email protected]"; 
      Process.Start(email); 
     } 
    } 
} 

仅供参考,请参阅this

+0

+1为更详细的链接,并显示所有的代码来确定你的解决方案应该去哪里。 – Gaffi 2012-04-06 14:11:40

2

发件人对象是调用该方法的按钮:

var buttonId = ((Button)sender).ID; 
1

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

  1. 选择要连接的事件处理程序的控制。

  2. 在属性窗口中,单击事件按钮()。

  3. 单击您要处理的事件的名称。

  4. 在事件名称旁边的值部分中,单击下拉按钮以显示与您要处理的事件的方法签名相匹配的现有事件处理程序的列表。

5.从列表中选择适当的事件处理程序。 代码将被添加到表单中以将事件绑定到现有的事件处理程序。

相关问题