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
4

1 回答 1

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 来避免标签引发事件时出现异常。我应该提一下,可能会将 TryCast 转换为通用控件而不是特定的面板或标签,因为 BackColor 是从基类(控件)继承的属性

于 2012-08-31T10:51:34.747 回答