2012-04-06 32 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.從列表中選擇適當的事件處理程序。 代碼將被添加到表單中以將事件綁定到現有的事件處理程序。

相關問題