我有一个由面板,图片框和标签组成的“按钮”。
我已经禁用了图片框和标签,以保持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发布于 2012-08-31 18:51:35
也为标签设置相同的事件
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是从基类(控件)继承的属性
https://stackoverflow.com/questions/12213760
复制相似问题