我正在尝试这样做,如果您将鼠标悬停在面板上,它会改变其颜色,而当您将鼠标悬停在标签上时,面板不会改变其颜色或返回到默认值,让我展示一下图片我试图解释一下:
如果您没有将鼠标悬停在任何内容上,它将处于默认状态:
如果您将鼠标悬停在其中,面板颜色将会改变:
然后,如果您将鼠标悬停在标签中,面板颜色不应更改:
但是,它的作用是,如果您将鼠标悬停在标签上,面板颜色将恢复为默认颜色。这是我的代码:
Private Sub Panel13_MouseEnter(sender As Object, e As EventArgs) Handles Panel13.MouseEnter
Panel13.BackColor = Color.DarkGreen
End Sub
Private Sub Panel13_MouseLeave(sender As Object, e As EventArgs) Handles Panel13.MouseLeave
Panel13.BackColor = Color.LimeGreen
End Sub
如何解决这个问题?有什么方法可以让代码变得干净还是我必须复制并粘贴代码并替换单词?
最佳答案
您可以使用 GetChildAtPoint
检查鼠标光标是否位于子控件上方的方法。
将 MouseLeave
事件处理程序中的代码替换为以下内容:
Dim childControl = Panel13.GetChildAtPoint(Panel13.PointToClient(Cursor.Position))
If childControl Is Nothing Then
Panel13.BackColor = Color.LimeGreen
End If
关于vb.net - 如何避免将鼠标悬停在子控件上时触发 MouseLeave 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68151124/