468,133 Members | 1,649 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,133 developers. It's quick & easy.

Question on MouseUp/MouseDown events with form buttons

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
Nov 20 '05 #1
4 3995
"Colin McGuire" <co***********@lycos.co.uk> schrieb
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.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2
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
Nov 20 '05 #3
"Colin McGuire" <co***********@lycos.co.uk> schrieb
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.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #4
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
"Armin Zingler" <az*******@freenet.de> wrote in message news:<ej**************@TK2MSFTNGP12.phx.gbl>...
"Colin McGuire" <co***********@lycos.co.uk> schrieb
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.

Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Stephen Saunders | last post: by
6 posts views Thread by Dave | last post: by
5 posts views Thread by Tim Bücker | last post: by
1 post views Thread by Nathan Sokalski | last post: by
1 post views Thread by Alan | last post: by
8 posts views Thread by Louis | last post: by
12 posts views Thread by Tom Bean | last post: by
27 posts views Thread by didacticone | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.