System.Timers.Timer question ???

G

Guest

Hi,
I'm have a datagrid, and I'm trying to have a tooltip pop up if a cell has
been hovered on for 2 seconds. I was thinking of using DataGrid.Hover, but
then decided to try this instead:

static System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
private void dg_MouseMove(object sender, System.Windows.Forms.MouseEventArgs
e)
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 2000;
timer.Start();
DataGrid.HitTestInfo hitInfo = dg.HitTest(e.X, e.Y);
eX = hitInfo.Column; eY = hitInfo.Row;
timer.Elapsed+=new System.Timers.ElapsedEventHandler(OnTimedEvent);
timer.AutoReset = false;
timer.Enabled = true;
}
private static void OnTimedEvent(object source,
System.Timers.ElapsedEventArgs e)
{
MessageBox.Show("2 secs");
....add tooltip stuff here...
}

When I run it, it does wait 2 seconds before the messagebox comes up, but
then a new messagebox pops up every 1/2 second or so (even if I don't move
the mouse again). I was under the impression that timer.Autoselect would
prevent that from happening. I think I have other questions too, but I'd
like to get this step working before I do anything else.

Thanks!!!!
Mel
 
K

Kevin Spencer

Have you any idea how many "mousemove" events are fired when you move your
mouse over an interface object? Quite a few. It just depends on how long you
move your mouse over the object. If you only want to handle the event once,
you need to use some sort of boolean variable that is checked before
execting the MessageBox code. The handler would check the value, see if it
is false, and if not, set it to false, and execute code. If you want it to
fire more than once, you also need to have a way of turning it back on. How
you do this is up to you.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but you can't make it stink.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top