Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

c# - MouseHover and MouseLeave don't work when the control is invisible

For some reason MouseHover and MouseLeave functions behave really strange. All I need to do is, when the cursor is over the "button", I want to make the button visible and when the cursor leaves the button, I want to make it invisible. Whatever I try I couldn't make it work. It seems like Mouse events not working when the control object is invisible.

private void button1_MouseHover(object sender, EventArgs e)
{
   button1.Visible = true;
}

private void button1_MouseLeave(object sender, EventArgs e)
{
    button1.Visible = false;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well... that's how it works. Continue handling the button's MouseLeave event and handle MouseMove for its parent (I assume the form):

private void Form_MouseMove(object sender, MouseEventArgs e) {
    if (button1.Bounds.Contains(e.Location) && !button1.Visible) {
        button1.Show();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...