How to fix the event delay

2020-02-06 17:37发布

问题:

I'm making a table reservation system for a school project, and I'm using a list of PictureBoxes to represent the tables. To these PictureBoxes I have linked a hover event, and when I hover the BackColor property is changed.

List<PictureBox> pb = new List<PictureBox> { pictureBox1, pictureBox2, pictureBox3};

foreach (PictureBox p in pb)
{
    p.BorderStyle = BorderStyle.Fixed3D;
    p.BackColor = Color.White;
    p.MouseHover += new EventHandler(mouseOn);
}

private void mouseOn(object sender, EventArgs e)
{
    ((PictureBox)sender).BackColor = Color.Green;
}

Everything works great, except that when I hover the mouse over, it takes 1 second before the event is triggerd, is there any way to trigger the event immidiately?

回答1:

If you want to trigger event immediately, use MouseEnter event instead. By design your mouse should stay stationary for some time for MouseHover event to fire.

BTW SystemInformation.MouseHoverTime holds that delay for MouseHover event.



回答2:

everything works great, except that when i haver the mouse over, it takes 1 second before the event is triggered

That's how Control.MouseHover is defined:

Occurs when the mouse pointer rests on the control.

The "rests" part is the delay of a second. I don't know of any way of adjusting the length of time that the mouse has to rest of a control before it counts as a hover.

If you don't want any delay - i.e. you want an event which is raised as soon as the mouse enters the region of the control - you should be using Control.MouseEnter instead.

From the documentation of both events:

Mouse events occur in the following order:

  • MouseEnter

  • MouseMove

  • MouseHover / MouseDown / MouseWheel

  • MouseUp

  • MouseLeave