2012-08-31 69 views
0

我有一個由面板,圖片框和標籤組成的「按鈕」。在標籤上禁用鼠標事件(啓用時)

我已禁用圖片框和標籤以保持MouseEnter(和背景顏色)處於活動狀態。 但是,禁用的標籤具有通常的淡化文本 - 是否有一種方法可以使標籤保持啓用狀態,但忽略鼠標事件,就像禁用時一樣?

Private Sub panelBackup_MouseEnter(sender As Object, e As EventArgs) Handles panelBackupButton.MouseEnter 
    Dim btn As Panel = DirectCast(sender, Panel) 
    btn.BackColor = Color.Gray 
End Sub 

Private Sub panelBackup_MouseLeave(sender As Object, e As EventArgs) Handles panelBackupButton.MouseLeave 
    Dim btn As Panel = DirectCast(sender, Panel) 
    btn.BackColor = Color.LightGray 
End Sub 

回答

1

設置同樣的事件也爲標籤

Private Sub panelBackup_MouseEnter(sender As Object, e As EventArgs) 
        Handles panelBackupButton.MouseEnter, Label1.MouseEnter 
    Dim btn As Panel = TryCast(sender, Panel) 
    if Not btn Is Nothing then 
     btn.BackColor = Color.Gray 
    else 
     Dim lbl As Label = TryCast(sender, Label) 
     lbl.BackColor = Color.Gray 
    end if 
End Sub 

Private Sub panelBackup_MouseLeave(sender As Object, e As EventArgs) 
        Handles panelBackupButton.MouseLeave, Label1.MouseLeave 
    Dim btn As Panel = TryCast(sender, Panel) 
    if Not btn Is Nothing then 
     btn.BackColor = Color.LightGray 
    else 
     Dim lbl As Label = TryCast(sender, Label) 
     lbl.BackColor = Color.LightGray 
    end if 
End Sub 

而且我已經刪除了DirectCast,否則你需要添加不必要的額外的邏輯來區分,你可以直接使用panelBackup.BackColor財產(由專家組或標籤發起的事件

編輯:看到您的評論我已經改變了重新引入演員表的方法,但使用TryCast避免了由標籤引發事件時的異常 我應該menti因爲BackColor是從基類繼承的屬性(控制)

+0

嗨,該功能用於多個面板,因此我試圖施放。 – madlan