Question on MouseUp/MouseDown events with form buttons

C

Colin McGuire

Hi again, thanks everyone for your previous help. But having resolved
past problems, I'm moving on to new problems :(

This one is a simple winforms application with two buttons, named
Button1 and Button2, on it (code below). When I press the mouse button
over Button2, and don't release it, in the IDE output window it prints
"Button2_MouseDown" only. If I release the mouse button over the
button, over the form, or even over ANY other application that is
running I also see "Button2_MouseUp" in the output window. This is
what I want and expected.

But when I do exactly the same for Button1, I don't see this. I press
button1, don't release the mouse button, a yellow form is displayed,
and what I am wanting is that when I release the mousebutton the
yellow form is hidden/disappear. Depending on where I release the
mouse button seems to change whether the "MouseUp" event is fired and
therefore whether I hide my yellow form. Why? For example, press the
left mouse button while over Button1, and keep the mousebutton
depressed, a yellow backcolor form will be displayed, now move the
cursor somewhere (over the desktop) still holding down the mouse
button, release the mouse button, and the "MouseUp" event isn't fired!
Ha? I want it to be fired so I can hide the yellow form. So there has
to be a trick - but what is it.

Thank you again
Colin



Public Class Form1
Inherits System.Windows.Forms.Form

//and include the Windows Form Designer Code in here

Dim WithEvents f As Form

Private Sub Button1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Button1.MouseDown
Debug.WriteLine("Button1_MouseDown")
f = New Form
f.Size = New Size(80, 80)
f.BackColor = Color.Yellow
f.Show()
End Sub

Private Sub Button1_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Button1.MouseUp
Debug.WriteLine("Button1_MouseUp")
f.Close()
f.Hide()
f = Nothing
End Sub

Private Sub Button2_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Button2.MouseDown
Debug.WriteLine("Button2_MouseDown")
End Sub

Private Sub Button2_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Button2.MouseUp
Debug.WriteLine("Button2_MouseUp")
End Sub
End Class
 
A

Armin Zingler

Colin McGuire said:
Hi again, thanks everyone for your previous help. But having
resolved past problems, I'm moving on to new problems :(

This one is a simple winforms application with two buttons, named
Button1 and Button2, on it (code below). When I press the mouse
button over Button2, and don't release it, in the IDE output window
it prints "Button2_MouseDown" only. If I release the mouse button
over the button, over the form, or even over ANY other application
that is running I also see "Button2_MouseUp" in the output window.
This is what I want and expected.

But when I do exactly the same for Button1, I don't see this. I
press button1, don't release the mouse button, a yellow form is
displayed, and what I am wanting is that when I release the
mousebutton the yellow form is hidden/disappear. Depending on where I
release the mouse button seems to change whether the "MouseUp" event
is fired and therefore whether I hide my yellow form. Why? For
example, press the left mouse button while over Button1, and keep the
mousebutton depressed, a yellow backcolor form will be displayed, now
move the cursor somewhere (over the desktop) still holding down the
mouse button, release the mouse button, and the "MouseUp" event isn't
fired! Ha? I want it to be fired so I can hide the yellow form. So
there has to be a trick - but what is it.

Whenever you press the mouse button, the control "captures" the mouse, so
all mouse messages will be sent to the button even if the cursor is outside
the button. As soon as you show the Form, the mouse is released. Untested
suggestion: Set Button1.Capture = True after showing the Form.
 
C

Colin McGuire

Thank you Armin - that works a treat, however because I didn't explain
things properly, it isn't what I am after :( My error.

Here is a second attempt at explaining what I am after, much more
specific that the general question I was asking previously. Basically
I want a glorified popup/context menu on a button that shows only when
the mouse button is pressed over the button, and disappears when the
mouse button is released anywhere. Here's a full description.

When the mouse button is depressed over Button1, I want the
popup form (an instance of formPopup) to show. If the mouse
button is released anywhere, the cursor could be anywhere,
even over another application or on the desktop or somewhere else,
I want the popup form to be hidden. The code I have means that
if I move the cursor away from the display form, then release
the mouse button, the form isn't always hidden. I also want
to trap various events in the instance of formPopup, such as
mouseenter etc for controls on that form. By adding
Button1.Capture=True, no events in the instance of formPopup
can be caught.

I hope this is more clear. Thanks again for your previous help & hope
you/others can help me out here.
Colin


Here is the full code I already have.

1. Launch Visual Studio .Net 2003 and create a new winforms
application.
2. Use the VS IDE toolbar to put a new button, Button1, on the form.
3. Paste in the following code

Public Class Form1
Inherits System.Windows.Forms.Form

//and include the Windows Form Designer Code in here

Public Class formPopup
Inherits System.Windows.Forms.Form

Private Sub formPopup_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim b1 As New Button
b1.Size = New Size(15, 15)
b1.Location = New Point(10, 10)
b1.BackColor = Color.Gray
Me.Controls.Add(b1)
AddHandler b1.MouseEnter, AddressOf subMEnter
AddHandler b1.MouseLeave, AddressOf subMLeave
AddHandler b1.MouseUp, AddressOf subMUp

Dim b2 As New Button
b2.Size = New Size(15, 15)
b2.Location = New Point(30, 10)
b2.BackColor = Color.Gray
Me.Controls.Add(b2)
AddHandler b2.MouseEnter, AddressOf subMEnter
AddHandler b2.MouseLeave, AddressOf subMLeave
End Sub

Private Sub subMUp(ByVal sender As Object, _
ByVal e As MouseEventArgs)
Debug.WriteLine("MouseUp")
End Sub

Private Sub subMLeave(ByVal sender As Object, _
ByVal e As EventArgs)
Dim b As Button = CType(sender, Button)
b.BackColor = Color.Gray
End Sub

Private Sub subMEnter(ByVal sender As Object, _
ByVal e As EventArgs)
Dim b As Button = CType(sender, Button)
b.BackColor = Color.Blue
End Sub
End Class


Dim WithEvents f As formPopup

Private Sub Button1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Button1.MouseDown
Debug.WriteLine("Button1_MouseDown")
f = New formPopup

f.Size = New Size(80, 80)
f.BackColor = Color.Yellow

f.Show()
'Button1.Capture = True 'Have to comment this or else I cannot
'trap mouse trap events for controls on the
form
End Sub

Private Sub Button1_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Button1.MouseUp, f.MouseUp
Debug.WriteLine("Button1_MouseUp")
f.Close()
f.Hide()
f = Nothing
End Sub
End Class
 
A

Armin Zingler

Colin McGuire said:
Dim WithEvents f As formPopup

Private Sub Button1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Button1.MouseDown
Debug.WriteLine("Button1_MouseDown")
f = New formPopup

f.Size = New Size(80, 80)
f.BackColor = Color.Yellow

f.Show()
'Button1.Capture = True 'Have to comment this or else I cannot
'trap mouse trap events for controls on
the
form
End Sub

Does setting

f.capture = True

help? I tried it and I think that's what you are looking for.
 
C

Colin McGuire

Thanks Armin but no it doesn't work either (the f.capture = True)
means that the buttons don't capture events (ie change from gray to
blue and back again).

:)

I've learnt a lot trying to do this and have spent the last two nights
trying to get it going. This weekend I will give it another go, try a
different approach.
Thanks again
Colin
 

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