2011-05-10 60 views
1

我問這個問題以前在vb.net每個控件數組,但我想這樣做在C#..事件在C#

Private Sub cbtns_ClickButtonArea(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbtn_a.ClickButtonArea, cbtn_b.ClickButtonArea, cbtn_c.ClickButtonArea, cbtn_d.ClickButtonArea 
Dim cbtn As CButtonLib.CButton() = {cbtn_a, cbtn_b, cbtn_c, cbtn_d}   
Dim clickedBtn As CButtonLib.CButton = DirectCast(sender, CButtonLib.CButton) 

For Each cb As CButtonLib.CButton In cbtn 
    If cb Is clickedBtn Then 
     cb.Enabled = False 
    Else 
     cb.Enabled = True 
    End If 
Next   
End Sub 

如何將其轉換爲C#?要添加

回答

2

像這樣的事情

private void cbtns_ClickButtonArea(System.Object Sender, System.Windows.Forms.MouseEventArgs e) 
{ 
    CButtonLib.CButton[] cbtn = { 
     cbtn_a, 
     cbtn_b, 
     cbtn_c, 
     cbtn_d 
    }; 
    CButtonLib.CButton clickedBtn = (CButtonLib.CButton)sender; 

    foreach (CButtonLib.CButton cb in cbtn) { 
     if (cb == clickedBtn)) { 
      cb.Enabled = false; 
     } else { 
      cb.Enabled = true; 
     } 
    } 
} 

cbtn_a.ClickButtonArea += cbtns_ClickButtonArea; 
cbtn_b.ClickButtonArea += cbtns_ClickButtonArea; 
cbtn_c.ClickButtonArea += cbtns_ClickButtonArea; 
cbtn_d.ClickButtonArea += cbtns_ClickButtonArea; 

因爲C#沒有和相當於Handles,事件處理程序都像它的上面顯示。