MouseClick OR MouseDoubleClick

J

JezB

I want a control to respond differently on a MouseClick or a
MouseDoubleClick event. Problem is, when the user double-clicks, the
MouseClick is fired FIRST, and there seems no way of knowing whether it's
the first part of a double-click to avoid doing the stuff that the single
click should do.

I'm sure I'm missing something pretty trivial, but what is it ?
 
G

Guest

I want a control to respond differently on a MouseClick or a
MouseDoubleClick event. Problem is, when the user double-clicks, the
MouseClick is fired FIRST, and there seems no way of knowing whether it's
the first part of a double-click to avoid doing the stuff that the single
click should do.
I'm sure I'm missing something pretty trivial, but what is it ?

I don't think you are missing a thing. I think there is no good way to
handle both clicks and double clicks. I suggest you consider shift+click,
alt+click, etc instead.
 
S

Stuart Carnie

Those events are really tied together.

Usually the click event is 'selecting' some object and the double click
event is invoking some default action.

Here's what I'd consider a 'hack', you could use a timer that was started
when you detected the initial click, and had a timeout set to the double
click time (REG:\HKEY_CURRENT_USER\Control Panel\Mouse\DoubleClickSpeed
(DWORD)), which is in milli-seconds.

So some pseudo C# code:

private bool m_doubleClick;
private Timer m_clickTimer = new Timer();

Initialize(..)
{
m_clickTimer.Interval = [value from REG:\HKEY_CURRENT_USER\Control
Panel\Mouse\DoubleClickSpeed] + 50;
// the 50 is so it's slightly more than the actual double click time, test
this
m_clickTimer.Elapsed += new ElapsedEventHandler(OnClickEventHandler);
}

void Form1_OnMouseClick(...)
{
m_doubleClick = false;
m_clickTimer.Enabled = true;
}

void From1_OnMouseDoubleClick(...)
{
m_doubleClick = true;
}

void OnClickEventHandler(...)
{
m_clickTimer.Enabled = false;
if (m_doubleClick)
{
// handle double click event
}
else
{
// handle single click
}
}

Cheers,

Stu
 
C

Claes Bergefall

Yes, a timer is the way to go if you need to use both single
and double click. However, there is no need to read the
registry to get the double click time. Simply use the
SystemInformation.DoubleClickTime property

/claes


Stuart Carnie said:
Those events are really tied together.

Usually the click event is 'selecting' some object and the double click
event is invoking some default action.

Here's what I'd consider a 'hack', you could use a timer that was started
when you detected the initial click, and had a timeout set to the double
click time (REG:\HKEY_CURRENT_USER\Control Panel\Mouse\DoubleClickSpeed
(DWORD)), which is in milli-seconds.

So some pseudo C# code:

private bool m_doubleClick;
private Timer m_clickTimer = new Timer();

Initialize(..)
{
m_clickTimer.Interval = [value from REG:\HKEY_CURRENT_USER\Control
Panel\Mouse\DoubleClickSpeed] + 50;
// the 50 is so it's slightly more than the actual double click time, test
this
m_clickTimer.Elapsed += new ElapsedEventHandler(OnClickEventHandler);
}

void Form1_OnMouseClick(...)
{
m_doubleClick = false;
m_clickTimer.Enabled = true;
}

void From1_OnMouseDoubleClick(...)
{
m_doubleClick = true;
}

void OnClickEventHandler(...)
{
m_clickTimer.Enabled = false;
if (m_doubleClick)
{
// handle double click event
}
else
{
// handle single click
}
}

Cheers,

Stu

JezB said:
I want a control to respond differently on a MouseClick or a
MouseDoubleClick event. Problem is, when the user double-clicks, the
MouseClick is fired FIRST, and there seems no way of knowing whether it's
the first part of a double-click to avoid doing the stuff that the single
click should do.

I'm sure I'm missing something pretty trivial, but what is it ?
 
S

Stuart Carnie

I did look for it - but didn't try there :)

Claes Bergefall said:
Yes, a timer is the way to go if you need to use both single
and double click. However, there is no need to read the
registry to get the double click time. Simply use the
SystemInformation.DoubleClickTime property

/claes


Stuart Carnie said:
Those events are really tied together.

Usually the click event is 'selecting' some object and the double click
event is invoking some default action.

Here's what I'd consider a 'hack', you could use a timer that was started
when you detected the initial click, and had a timeout set to the double
click time (REG:\HKEY_CURRENT_USER\Control Panel\Mouse\DoubleClickSpeed
(DWORD)), which is in milli-seconds.

So some pseudo C# code:

private bool m_doubleClick;
private Timer m_clickTimer = new Timer();

Initialize(..)
{
m_clickTimer.Interval = [value from REG:\HKEY_CURRENT_USER\Control
Panel\Mouse\DoubleClickSpeed] + 50;
// the 50 is so it's slightly more than the actual double click time, test
this
m_clickTimer.Elapsed += new ElapsedEventHandler(OnClickEventHandler);
}

void Form1_OnMouseClick(...)
{
m_doubleClick = false;
m_clickTimer.Enabled = true;
}

void From1_OnMouseDoubleClick(...)
{
m_doubleClick = true;
}

void OnClickEventHandler(...)
{
m_clickTimer.Enabled = false;
if (m_doubleClick)
{
// handle double click event
}
else
{
// handle single click
}
}

Cheers,

Stu

JezB said:
I want a control to respond differently on a MouseClick or a
MouseDoubleClick event. Problem is, when the user double-clicks, the
MouseClick is fired FIRST, and there seems no way of knowing whether
it's
the first part of a double-click to avoid doing the stuff that the
single
click should do.

I'm sure I'm missing something pretty trivial, but what is it ?
 
J

JezB

Cheers guys

Stuart Carnie said:
Those events are really tied together.

Usually the click event is 'selecting' some object and the double click
event is invoking some default action.

Here's what I'd consider a 'hack', you could use a timer that was started
when you detected the initial click, and had a timeout set to the double
click time (REG:\HKEY_CURRENT_USER\Control Panel\Mouse\DoubleClickSpeed
(DWORD)), which is in milli-seconds.

So some pseudo C# code:

private bool m_doubleClick;
private Timer m_clickTimer = new Timer();

Initialize(..)
{
m_clickTimer.Interval = [value from REG:\HKEY_CURRENT_USER\Control
Panel\Mouse\DoubleClickSpeed] + 50;
// the 50 is so it's slightly more than the actual double click time,
test this
m_clickTimer.Elapsed += new ElapsedEventHandler(OnClickEventHandler);
}

void Form1_OnMouseClick(...)
{
m_doubleClick = false;
m_clickTimer.Enabled = true;
}

void From1_OnMouseDoubleClick(...)
{
m_doubleClick = true;
}

void OnClickEventHandler(...)
{
m_clickTimer.Enabled = false;
if (m_doubleClick)
{
// handle double click event
}
else
{
// handle single click
}
}

Cheers,

Stu

JezB said:
I want a control to respond differently on a MouseClick or a
MouseDoubleClick event. Problem is, when the user double-clicks, the
MouseClick is fired FIRST, and there seems no way of knowing whether it's
the first part of a double-click to avoid doing the stuff that the single
click should do.

I'm sure I'm missing something pretty trivial, but what is it ?
 

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