|
-
Feb 9th, 2026, 07:44 AM
#1
Thread Starter
Lively Member
Why does Button.DoubleClick not fire in WinForms when MouseUp opens another form?
In a VB.NET Winforms application, we have a button.
On button click i.e. Button1.MouseUp event, it opens another form.
But when we double click on the button, the double click splits into two single clicks i.e. first single click opens another form (Button1.MouseUp event) and second single click happens on the now opened form which shows a message (MyBase.MouseUp event).
Code:
Public Class Form1
Private Sub Button1_MouseUp(sender As Object, e As EventArgs) Handles Button1.MouseUp
Dim form = New Form1
form.ShowDialog()
form.Dispose()
End Sub
Private Sub Button1_DClick(sender As Object, e As EventArgs) Handles Button1.DoubleClick
MsgBox("double click")
End Sub
Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles MyBase.MouseUp
MsgBox("Mouse Up")
End Sub
End Class
Why does this unexpected behaviour happen and how to handle this?
Why isn't the Button1.DoubleClick event called on double clicking the button?
We tried giving sleep() before form.ShowDialog() but it made no difference. We also tried using PeekMessage in the child form's Load event to discard pending click messages from the message queue, but we are looking for alternative, cleaner methods.
Last edited by IT_Researcher; Feb 9th, 2026 at 08:02 AM.
-
Feb 9th, 2026, 08:11 AM
#2
Re: Why does Button.DoubleClick not fire in WinForms when MouseUp opens another form?
You wouldn’t generally use DoubleClick and MouseUp.
In most applications, you’d just use the Click Event alone…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 9th, 2026, 09:03 AM
#3
Re: Why does Button.DoubleClick not fire in WinForms when MouseUp opens another form?
-
Feb 9th, 2026, 12:42 PM
#4
Re: Why does Button.DoubleClick not fire in WinForms when MouseUp opens another form?
Using double click on a button is certainly weird, but the behavior you described sounds entirely right. As far as the computer is concerned, there's an eternity between the two clicks in a double click. No matter how fast you are, double clicks will register in several milliseconds, which is enough time for a modern computer to perform millions of actions. You see it as a double click, the computer has plenty of time between those two clicks to do whatever it wants.
So, since you ARE doing something on Mouse Up, the computer has plenty of time to pass along the mouse up from the first click. By that point, it doesn't even know that you are double clicking. It certainly wouldn't be a good design if it were to say, "well, that was one click and I'm supposed to do something on mouse up....but let me wait around to see whether or not the user is going to make it a double click." Instead, it is going to go ahead and do the mouse up operation, which will open a different form...which will shift focus to that second form by the time the second half of your double click has happened. That's exactly what you are reporting. The computer is seeing your double click as a single click on the first form, which opens a second and shifts focus to that new form, then a single click on that new form.
Getting around this will be pretty painful, which is one of the reason that double clicks are used sparingly. A double click is only one thing to you. To the computer, it is two things that get interpreted as one if the second thing happens within N milliseconds of the first, and then only if nothing else has happened between the first thing and the second.
The easiest option is to get away from using Mouse Up, but that doesn't totally solve it. One solution people use is to not use double click, but either use right clicks, wheel clicks, or Key+Click (shift, alt, ctl, etc), all of which are single events rather than the dual event of the double click.
If you insist on using the double click, then what you'd have to do is have the Mouse Up not happen except after some delay. Sleep won't do that. Sleep puts EVERYTHING to sleep, including the event handling and that's not what you want at all. What you'd have to do is start a time in the Mouse Up for a very short duration. That would give the double click time to happen (it still might not for reasons I would need to test, but perhaps somebody would already know). If the double click happens, then you stop the timer and do whatever you want on the double click. The timer Tick event would only be raised if the double click didn't happen and the timer was not stopped. So, if the tick happens, then you stop the timer (so that it doesn't tick a second time) and do the Mouse Up stuff.
Note that this is a terrible solution which you should only use as a last resort. The reason it is terrible is that the shortest usable interval on a timer is down around 30 ms, which means that if you do not get a double click, there would be at least a 30 ms pause before the Mouse Up happens. That slight stutter might not be noticeable, but if the time between clicks in a double click is long to a computer, 30 ms is an eternity of wasted time.
My usual boring signature: Nothing
 
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|