我有一个“按钮”,包括面板,图片框和标签。
我已禁用了图片框和标签,以保持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
答案 0 :(得分: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可能是通用的Control而不是特定的Panel或Label,因为BackColor是从基类继承的属性(Control)