By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,570 Members | 970 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,570 IT Pros & Developers. It's quick & easy.

REPOST: Popup form on button

P: n/a
Hi - this was posted last weekend and unfortunately not resolved. The
solutions that were posted almost worked but after another 5 days of
working on the code everynight, I am not further ahead.
If you do have any ideas I would really like to hear them.
Thanks
Colin

- 0 - 0 - 0 -

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.

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 #1
Share this Question
Share on Google+
18 Replies


P: n/a
"Colin McGuire" <co***********@lycos.co.uk> schrieb
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


AFAIR I suggested to use

f.capture = true

instead of

button1.capture = true

in my last reply.
--
Armin

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

Nov 20 '05 #2

P: n/a
Armin:
I too thought this was done and dusted. Looking at
the original thread it appears he also wants to
trap button events on the popup form.
Colin:
Try this (the code is below). The key code is in
formPopup_MouseLeave. If I understand the user
interface you are describing, you must have a really
good reason for wanting to code this behaviour. For
what it's worth, I find it annoying and not something
I would put into any of my applications.

Note that MouseLeave is not the only way to 'leave'
a control ! You will need to make it more robust.
Hexathioorthooxalate


Public Class Form1
Inherits System.Windows.Forms.Form

'[+]Windows Form Designer generated code

Public Class formPopup
Inherits System.Windows.Forms.Form

Public Event popupNotification()
Private _button As Button = Nothing

Public ReadOnly Property getButton() As Button
Get
Return _button
End Get
End Property

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
b1.Text = "1"
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
b2.Text = "2"

Me.Controls.Add(b2)
AddHandler b2.MouseEnter, AddressOf subMEnter
AddHandler b2.MouseLeave, AddressOf subMLeave
AddHandler b2.MouseUp, AddressOf subMUp
End Sub

Private Sub subMUp(ByVal sender As Object, _
ByVal e As MouseEventArgs) Handles MyBase.MouseUp
RaiseEvent popupNotification()
End Sub

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

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

Private Sub formPopup_MouseLeave(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.MouseLeave
Dim cursorPosition As Point = PointToClient(Cursor.Position())
Dim rect As Rectangle = Me.ClientRectangle
If Not rect.Contains(cursorPosition) Then
RaiseEvent popupNotification()
End If
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
Const FORMWIDTH As Integer = 80
Const FORMHEIGHT As Integer = 80
f = New formPopup
f.Size = New Size(FORMWIDTH, FORMHEIGHT)
f.BackColor = Color.Yellow
f.FormBorderStyle = FormBorderStyle.FixedSingle
f.ControlBox = False
f.StartPosition = FormStartPosition.Manual
f.Location = New Point(Cursor.Position.X - FORMWIDTH \ 2, _
Cursor.Position.Y - FORMHEIGHT \ 2)
f.Show()
End Sub

Private Sub disposeOfPopupForm()
If Not Nothing Is f Then
f.Close()
f.Hide()
f = Nothing
End If
End Sub

Private Sub f_popupNotification() Handles f.popupNotification
Dim buttonPressed As Button = f.getButton
Call disposeOfPopupForm()
If Nothing Is buttonPressed Then
Debug.WriteLine("no button was pressed")
Else
Debug.WriteLine("Selected button: " + buttonPressed.Text)
End If
End Sub

End Class


"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
"Colin McGuire" <co***********@lycos.co.uk> schrieb
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


AFAIR I suggested to use

f.capture = true

instead of

button1.capture = true

in my last reply.
--
Armin

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


Nov 20 '05 #3

P: n/a

Try this out Colin. It worked when I tried it. You need to set the button
capture to True. Then create a rect of the button to see if the point is in
or out of the button. Also, why do you have Button1_MouseUp handling the
f.mouseup? Remove it and it will work.
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()

'-- You NEED to set this to true!
Button1.Capture = True

End Sub

Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) _
Handles Button1.MouseUp ' ----------- f.MouseUp <-------- Get
rid of this. Why is this here?
Debug.WriteLine("Button1_MouseUp")

'-- Create a rect of the button
Dim rect As New Rectangle(0, 0, Button1.Width, Button1.Height)

'-- If mouse point isn't in the rect close it
If Not rect.Contains(e.X, e.Y) Then
f.Close()
f.Hide()
f = Nothing
End If

'-- Release Button capture
Button1.Capture = False

End Sub



"Colin McGuire" <co***********@lycos.co.uk> wrote in message
news:ab**************************@posting.google.c om...
Hi - this was posted last weekend and unfortunately not resolved. The
solutions that were posted almost worked but after another 5 days of
working on the code everynight, I am not further ahead.
If you do have any ideas I would really like to hear them.
Thanks
Colin

- 0 - 0 - 0 -

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.

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 #4

P: n/a
Brian, I'm not sure your code does exactly what he wants. This is the
confusion I think: from his posting I believe he wants to "select" buttons
on the popup form while the mouse button is down, and further only display
the popup form while the mouse button is down. If the mouse button is ever
released, regardless of where it is, then the popup form is hidden/disposed
of.
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.

Colin, could you reply to confirm your exact desired behaviour please.

Hexathioorthooxalate

"Brian" <no****@prairie.lakes.com> wrote in message
news:vr************@corp.supernews.com...
Try this out Colin. It worked when I tried it. You need to set the button
capture to True. Then create a rect of the button to see if the point is in or out of the button. Also, why do you have Button1_MouseUp handling the
f.mouseup? Remove it and it will work.
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()

'-- You NEED to set this to true!
Button1.Capture = True

End Sub

Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) _
Handles Button1.MouseUp ' ----------- f.MouseUp <-------- Get rid of this. Why is this here?
Debug.WriteLine("Button1_MouseUp")

'-- Create a rect of the button
Dim rect As New Rectangle(0, 0, Button1.Width, Button1.Height)

'-- If mouse point isn't in the rect close it
If Not rect.Contains(e.X, e.Y) Then
f.Close()
f.Hide()
f = Nothing
End If

'-- Release Button capture
Button1.Capture = False

End Sub



"Colin McGuire" <co***********@lycos.co.uk> wrote in message
news:ab**************************@posting.google.c om...
Hi - this was posted last weekend and unfortunately not resolved. The
solutions that were posted almost worked but after another 5 days of
working on the code everynight, I am not further ahead.
If you do have any ideas I would really like to hear them.
Thanks
Colin

- 0 - 0 - 0 -

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.

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 #5

P: n/a
What? How do you select another button when the a button is already down?
You have to release the button before you can select another. It seems to me
like he's trying to create a custom popup menu that works like the standard
Cut/Paste/SelectAll menu but with buttons. Is this right?

"hexathioorthooxalate" <ru***@REMOVESPAM.clara.co.uk> wrote in message
news:10***************@damia.uk.clara.net...
Brian, I'm not sure your code does exactly what he wants. This is the
confusion I think: from his posting I believe he wants to "select" buttons on the popup form while the mouse button is down, and further only display
the popup form while the mouse button is down. If the mouse button is ever
released, regardless of where it is, then the popup form is hidden/disposed of.
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.
Colin, could you reply to confirm your exact desired behaviour please.

Hexathioorthooxalate

"Brian" <no****@prairie.lakes.com> wrote in message
news:vr************@corp.supernews.com...

Try this out Colin. It worked when I tried it. You need to set the

button capture to True. Then create a rect of the button to see if the point is

in
or out of the button. Also, why do you have Button1_MouseUp handling the
f.mouseup? Remove it and it will work.
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()

'-- You NEED to set this to true!
Button1.Capture = True

End Sub

Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) _
Handles Button1.MouseUp ' ----------- f.MouseUp <--------

Get
rid of this. Why is this here?
Debug.WriteLine("Button1_MouseUp")

'-- Create a rect of the button
Dim rect As New Rectangle(0, 0, Button1.Width, Button1.Height)

'-- If mouse point isn't in the rect close it
If Not rect.Contains(e.X, e.Y) Then
f.Close()
f.Hide()
f = Nothing
End If

'-- Release Button capture
Button1.Capture = False

End Sub



"Colin McGuire" <co***********@lycos.co.uk> wrote in message
news:ab**************************@posting.google.c om...
Hi - this was posted last weekend and unfortunately not resolved. The
solutions that were posted almost worked but after another 5 days of
working on the code everynight, I am not further ahead.
If you do have any ideas I would really like to hear them.
Thanks
Colin

- 0 - 0 - 0 -

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.

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 #6

P: n/a
"Brian" <no****@prairie.lakes.com> wrote in message
news:vr************@corp.supernews.com...
What? How do you select another button when the a button is already down?
You have to release the button before you can select another. It seems to me

Yes - I think he is wanting something like this although clearly as we can
both see, he can't select the button via normal button clicks. I think it
explains why he says he wants to capture MouseOver events for controls on
the popup form, rather than click events.

I'm still waiting for Colin to repost and confirm exactly what he wants.

Hexathioorthooxalate
like he's trying to create a custom popup menu that works like the standard Cut/Paste/SelectAll menu but with buttons. Is this right?

"hexathioorthooxalate" <ru***@REMOVESPAM.clara.co.uk> wrote in message
news:10***************@damia.uk.clara.net...
Brian, I'm not sure your code does exactly what he wants. This is the
confusion I think: from his posting I believe he wants to "select" buttons
on the popup form while the mouse button is down, and further only display the popup form while the mouse button is down. If the mouse button is ever released, regardless of where it is, then the popup form is

hidden/disposed
of.
> 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.


Colin, could you reply to confirm your exact desired behaviour please.

Hexathioorthooxalate

"Brian" <no****@prairie.lakes.com> wrote in message
news:vr************@corp.supernews.com...

Try this out Colin. It worked when I tried it. You need to set the button capture to True. Then create a rect of the button to see if the point is in
or out of the button. Also, why do you have Button1_MouseUp handling
the f.mouseup? Remove it and it will work.
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()

'-- You NEED to set this to true!
Button1.Capture = True

End Sub

Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) _
Handles Button1.MouseUp ' ----------- f.MouseUp <-------- Get
rid of this. Why is this here?
Debug.WriteLine("Button1_MouseUp")

'-- Create a rect of the button
Dim rect As New Rectangle(0, 0, Button1.Width, Button1.Height)

'-- If mouse point isn't in the rect close it
If Not rect.Contains(e.X, e.Y) Then
f.Close()
f.Hide()
f = Nothing
End If

'-- Release Button capture
Button1.Capture = False

End Sub



"Colin McGuire" <co***********@lycos.co.uk> wrote in message
news:ab**************************@posting.google.c om...
> Hi - this was posted last weekend and unfortunately not resolved.

The > solutions that were posted almost worked but after another 5 days of
> working on the code everynight, I am not further ahead.
> If you do have any ideas I would really like to hear them.
> Thanks
> Colin
>
> - 0 - 0 - 0 -
>
> 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.
>
> 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 #7

P: n/a
Ok, let's try this. This acts like a context menu with buttons. If this
isn't it, I don't know what he wants.

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")
Dim pt As Point
Dim pt2 As Point
f = New formPopup()

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

f.StartPosition = FormStartPosition.Manual

f.Location = pt
f.Show()

Cursor.Position = f.PointToScreen(New Point(0, 0))

End Sub

End Class

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)
Me.FormBorderStyle = FormBorderStyle.None
AddHandler b1.MouseEnter, AddressOf subMEnter
AddHandler b1.MouseLeave, AddressOf subMLeave
AddHandler b1.MouseUp, AddressOf subMUp

AddHandler Me.Leave, AddressOf subLeaving

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)

Dim b3 As New Button()
b3.Size = New Size(15, 15)
b3.Location = New Point(50, 10)
b3.BackColor = Color.Gray
Me.Controls.Add(b3)

Dim b4 As New Button()
b4.Size = New Size(15, 15)
b4.Location = New Point(70, 10)
b4.BackColor = Color.Gray
Me.Controls.Add(b4)

AddHandler b2.MouseEnter, AddressOf subMEnter
AddHandler b2.MouseLeave, AddressOf subMLeave
AddHandler b3.MouseEnter, AddressOf subMEnter
AddHandler b3.MouseLeave, AddressOf subMLeave
AddHandler b4.MouseEnter, AddressOf subMEnter
AddHandler b4.MouseLeave, AddressOf subMLeave
End Sub
Private Sub subLeaving(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Leave
Me.Close()
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



"hexathioorthooxalate" <ru***@REMOVESPAM.clara.co.uk> wrote in message
news:10****************@damia.uk.clara.net...
"Brian" <no****@prairie.lakes.com> wrote in message
news:vr************@corp.supernews.com...
What? How do you select another button when the a button is already down?
You have to release the button before you can select another. It seems
to me

Yes - I think he is wanting something like this although clearly as we can
both see, he can't select the button via normal button clicks. I think it
explains why he says he wants to capture MouseOver events for controls on
the popup form, rather than click events.

I'm still waiting for Colin to repost and confirm exactly what he wants.

Hexathioorthooxalate
like he's trying to create a custom popup menu that works like the standard
Cut/Paste/SelectAll menu but with buttons. Is this right?

"hexathioorthooxalate" <ru***@REMOVESPAM.clara.co.uk> wrote in message
news:10***************@damia.uk.clara.net...
Brian, I'm not sure your code does exactly what he wants. This is the
confusion I think: from his posting I believe he wants to "select"

buttons
on the popup form while the mouse button is down, and further only

display the popup form while the mouse button is down. If the mouse button is ever released, regardless of where it is, then the popup form is

hidden/disposed
of.

> > 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.

Colin, could you reply to confirm your exact desired behaviour please.

Hexathioorthooxalate

"Brian" <no****@prairie.lakes.com> wrote in message
news:vr************@corp.supernews.com...
>
> Try this out Colin. It worked when I tried it. You need to set the

button
> capture to True. Then create a rect of the button to see if the point is
in
> or out of the button. Also, why do you have Button1_MouseUp handling the > f.mouseup? Remove it and it will work.
>
>
> 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()
>
> '-- You NEED to set this to true!
> Button1.Capture = True
>
> End Sub
>
>
>
> Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As
> System.Windows.Forms.MouseEventArgs) _
> Handles Button1.MouseUp ' ----------- f.MouseUp <-------- Get
> rid of this. Why is this here?
>
>
> Debug.WriteLine("Button1_MouseUp")
>
> '-- Create a rect of the button
> Dim rect As New Rectangle(0, 0, Button1.Width, Button1.Height)
>
> '-- If mouse point isn't in the rect close it
> If Not rect.Contains(e.X, e.Y) Then
> f.Close()
> f.Hide()
> f = Nothing
> End If
>
> '-- Release Button capture
> Button1.Capture = False
>
> End Sub
>
>
>
>
>
>
>
> "Colin McGuire" <co***********@lycos.co.uk> wrote in message
> news:ab**************************@posting.google.c om...
> > Hi - this was posted last weekend and unfortunately not resolved. The > > solutions that were posted almost worked but after another 5 days
of > > working on the code everynight, I am not further ahead.
> > If you do have any ideas I would really like to hear them.
> > Thanks
> > Colin
> >
> > - 0 - 0 - 0 -
> >
> > 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.
> >
> > 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 #8

P: n/a
This looks good, he has more than enough to work with now.

I sent him through an eMail message a few hours ago but it's a dummy account
he probably uses to control spam (it was rejected). I look forward to him
replying confirming the detail.

Cheers Brian
Hexathioorthooxalate


"Brian" <no****@prairie.lakes.com> wrote in message
news:vr************@corp.supernews.com...
Ok, let's try this. This acts like a context menu with buttons. If this
isn't it, I don't know what he wants.

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")
Dim pt As Point
Dim pt2 As Point
f = New formPopup()

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

f.StartPosition = FormStartPosition.Manual

f.Location = pt
f.Show()

Cursor.Position = f.PointToScreen(New Point(0, 0))

End Sub

End Class

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)
Me.FormBorderStyle = FormBorderStyle.None
AddHandler b1.MouseEnter, AddressOf subMEnter
AddHandler b1.MouseLeave, AddressOf subMLeave
AddHandler b1.MouseUp, AddressOf subMUp

AddHandler Me.Leave, AddressOf subLeaving

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)

Dim b3 As New Button()
b3.Size = New Size(15, 15)
b3.Location = New Point(50, 10)
b3.BackColor = Color.Gray
Me.Controls.Add(b3)

Dim b4 As New Button()
b4.Size = New Size(15, 15)
b4.Location = New Point(70, 10)
b4.BackColor = Color.Gray
Me.Controls.Add(b4)

AddHandler b2.MouseEnter, AddressOf subMEnter
AddHandler b2.MouseLeave, AddressOf subMLeave
AddHandler b3.MouseEnter, AddressOf subMEnter
AddHandler b3.MouseLeave, AddressOf subMLeave
AddHandler b4.MouseEnter, AddressOf subMEnter
AddHandler b4.MouseLeave, AddressOf subMLeave
End Sub
Private Sub subLeaving(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Leave
Me.Close()
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



"hexathioorthooxalate" <ru***@REMOVESPAM.clara.co.uk> wrote in message
news:10****************@damia.uk.clara.net...
"Brian" <no****@prairie.lakes.com> wrote in message
news:vr************@corp.supernews.com...
What? How do you select another button when the a button is already down? You have to release the button before you can select another. It seems to
me

Yes - I think he is wanting something like this although clearly as we can both see, he can't select the button via normal button clicks. I think it explains why he says he wants to capture MouseOver events for controls on the popup form, rather than click events.

I'm still waiting for Colin to repost and confirm exactly what he wants.

Hexathioorthooxalate
like he's trying to create a custom popup menu that works like the standard
Cut/Paste/SelectAll menu but with buttons. Is this right?

"hexathioorthooxalate" <ru***@REMOVESPAM.clara.co.uk> wrote in message
news:10***************@damia.uk.clara.net...
> Brian, I'm not sure your code does exactly what he wants. This is the > confusion I think: from his posting I believe he wants to "select"

buttons

> on the popup form while the mouse button is down, and further only

display
> the popup form while the mouse button is down. If the mouse button is
ever
> released, regardless of where it is, then the popup form is
hidden/disposed
> of.
>
> > > 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.
>
> Colin, could you reply to confirm your exact desired behaviour please. >
> Hexathioorthooxalate
>
>
>
> "Brian" <no****@prairie.lakes.com> wrote in message
> news:vr************@corp.supernews.com...
> >
> > Try this out Colin. It worked when I tried it. You need to set the
button
> > capture to True. Then create a rect of the button to see if the

point
is
> in
> > or out of the button. Also, why do you have Button1_MouseUp handling the
> > f.mouseup? Remove it and it will work.
> >
> >
> > 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()
> >
> > '-- You NEED to set this to true!
> > Button1.Capture = True
> >
> > End Sub
> >
> >
> >
> > Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As
> > System.Windows.Forms.MouseEventArgs) _
> > Handles Button1.MouseUp ' ----------- f.MouseUp

<--------
> Get
> > rid of this. Why is this here?
> >
> >
> > Debug.WriteLine("Button1_MouseUp")
> >
> > '-- Create a rect of the button
> > Dim rect As New Rectangle(0, 0, Button1.Width, Button1.Height)
> >
> > '-- If mouse point isn't in the rect close it
> > If Not rect.Contains(e.X, e.Y) Then
> > f.Close()
> > f.Hide()
> > f = Nothing
> > End If
> >
> > '-- Release Button capture
> > Button1.Capture = False
> >
> > End Sub
> >
> >
> >
> >
> >
> >
> >
> > "Colin McGuire" <co***********@lycos.co.uk> wrote in message
> > news:ab**************************@posting.google.c om...
> > > Hi - this was posted last weekend and unfortunately not
resolved. The
> > > solutions that were posted almost worked but after another 5
days of > > > working on the code everynight, I am not further ahead.
> > > If you do have any ideas I would really like to hear them.
> > > Thanks
> > > Colin
> > >
> > > - 0 - 0 - 0 -
> > >
> > > 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.
> > >
> > > 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 #9

P: n/a
OK, I think i got it (maybe).

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")
Dim pt As Point
Dim pt2 As Point
f = New formPopup()

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

f.StartPosition = FormStartPosition.Manual
f.Capture = True
f.Location = pt
f.Show()

Cursor.Position = f.PointToScreen(New Point(0, 0))

End Sub

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

End Sub
End Class

Public Class formPopup
Inherits System.Windows.Forms.Form

Dim rect As Rectangle
Dim rect1 As Rectangle

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)
Me.FormBorderStyle = FormBorderStyle.None

AddHandler Me.MouseUp, AddressOf fMouseUp
AddHandler b1.MouseEnter, AddressOf subMEnter
AddHandler b1.MouseLeave, AddressOf subMLeave
AddHandler b1.MouseUp, AddressOf subMUp
AddHandler Me.MouseMove, AddressOf fMouseMove

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

rect = New Rectangle(b1.Left, b1.Top, b1.Width, b1.Height)
rect1 = New Rectangle(b2.Left, b2.Top, b2.Width, b2.Height)

End Sub

Private Sub fMouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove

If rect.Contains(e.X, e.Y) OrElse rect1.Contains(e.X, e.Y) Then
If Not Me.Capture = False Then
Me.Capture = False
End If
ElseIf Me.Capture = False Then
Me.Capture = True
End If

End Sub

Private Sub fMouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp

Me.Capture = False
Me.Close()
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
Nov 20 '05 #10

P: n/a
OK, I think i got it (maybe).

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")
Dim pt As Point
Dim pt2 As Point
f = New formPopup()

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

f.StartPosition = FormStartPosition.Manual
f.Capture = True
f.Location = pt
f.Show()

Cursor.Position = f.PointToScreen(New Point(0, 0))

End Sub

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

End Sub
End Class

Public Class formPopup
Inherits System.Windows.Forms.Form

Dim rect As Rectangle
Dim rect1 As Rectangle

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)
Me.FormBorderStyle = FormBorderStyle.None

AddHandler Me.MouseUp, AddressOf fMouseUp
AddHandler b1.MouseEnter, AddressOf subMEnter
AddHandler b1.MouseLeave, AddressOf subMLeave
AddHandler b1.MouseUp, AddressOf subMUp
AddHandler Me.MouseMove, AddressOf fMouseMove

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

rect = New Rectangle(b1.Left, b1.Top, b1.Width, b1.Height)
rect1 = New Rectangle(b2.Left, b2.Top, b2.Width, b2.Height)

End Sub

Private Sub fMouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove

If rect.Contains(e.X, e.Y) OrElse rect1.Contains(e.X, e.Y) Then
If Not Me.Capture = False Then
Me.Capture = False
End If
ElseIf Me.Capture = False Then
Me.Capture = True
End If

End Sub

Private Sub fMouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp

Me.Capture = False
Me.Close()
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
Nov 20 '05 #11

P: n/a
Hex, this is exactly what I was wanting.
Thank you
Colin

"hexathioorthooxalate" <ru***@REMOVESPAM.clara.co.uk> wrote in message news:<10***************@damia.uk.clara.net>...
Armin:
I too thought this was done and dusted. Looking at
the original thread it appears he also wants to
trap button events on the popup form.
Colin:
Try this (the code is below). The key code is in
formPopup_MouseLeave. If I understand the user
interface you are describing, you must have a really
good reason for wanting to code this behaviour. For
what it's worth, I find it annoying and not something
I would put into any of my applications.

Note that MouseLeave is not the only way to 'leave'
a control ! You will need to make it more robust.
Hexathioorthooxalate


Public Class Form1
Inherits System.Windows.Forms.Form

'[+]Windows Form Designer generated code

Public Class formPopup
Inherits System.Windows.Forms.Form

Public Event popupNotification()
Private _button As Button = Nothing

Public ReadOnly Property getButton() As Button
Get
Return _button
End Get
End Property

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
b1.Text = "1"
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
b2.Text = "2"

Me.Controls.Add(b2)
AddHandler b2.MouseEnter, AddressOf subMEnter
AddHandler b2.MouseLeave, AddressOf subMLeave
AddHandler b2.MouseUp, AddressOf subMUp
End Sub

Private Sub subMUp(ByVal sender As Object, _
ByVal e As MouseEventArgs) Handles MyBase.MouseUp
RaiseEvent popupNotification()
End Sub

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

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

Private Sub formPopup_MouseLeave(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.MouseLeave
Dim cursorPosition As Point = PointToClient(Cursor.Position())
Dim rect As Rectangle = Me.ClientRectangle
If Not rect.Contains(cursorPosition) Then
RaiseEvent popupNotification()
End If
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
Const FORMWIDTH As Integer = 80
Const FORMHEIGHT As Integer = 80
f = New formPopup
f.Size = New Size(FORMWIDTH, FORMHEIGHT)
f.BackColor = Color.Yellow
f.FormBorderStyle = FormBorderStyle.FixedSingle
f.ControlBox = False
f.StartPosition = FormStartPosition.Manual
f.Location = New Point(Cursor.Position.X - FORMWIDTH \ 2, _
Cursor.Position.Y - FORMHEIGHT \ 2)
f.Show()
End Sub

Private Sub disposeOfPopupForm()
If Not Nothing Is f Then
f.Close()
f.Hide()
f = Nothing
End If
End Sub

Private Sub f_popupNotification() Handles f.popupNotification
Dim buttonPressed As Button = f.getButton
Call disposeOfPopupForm()
If Nothing Is buttonPressed Then
Debug.WriteLine("no button was pressed")
Else
Debug.WriteLine("Selected button: " + buttonPressed.Text)
End If
End Sub

End Class


"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
"Colin McGuire" <co***********@lycos.co.uk> schrieb
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


AFAIR I suggested to use

f.capture = true

instead of

button1.capture = true

in my last reply.
--
Armin

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

Nov 20 '05 #12

P: n/a
Hex, this is exactly what I was wanting.
Thank you
Colin

"hexathioorthooxalate" <ru***@REMOVESPAM.clara.co.uk> wrote in message news:<10***************@damia.uk.clara.net>...
Armin:
I too thought this was done and dusted. Looking at
the original thread it appears he also wants to
trap button events on the popup form.
Colin:
Try this (the code is below). The key code is in
formPopup_MouseLeave. If I understand the user
interface you are describing, you must have a really
good reason for wanting to code this behaviour. For
what it's worth, I find it annoying and not something
I would put into any of my applications.

Note that MouseLeave is not the only way to 'leave'
a control ! You will need to make it more robust.
Hexathioorthooxalate


Public Class Form1
Inherits System.Windows.Forms.Form

'[+]Windows Form Designer generated code

Public Class formPopup
Inherits System.Windows.Forms.Form

Public Event popupNotification()
Private _button As Button = Nothing

Public ReadOnly Property getButton() As Button
Get
Return _button
End Get
End Property

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
b1.Text = "1"
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
b2.Text = "2"

Me.Controls.Add(b2)
AddHandler b2.MouseEnter, AddressOf subMEnter
AddHandler b2.MouseLeave, AddressOf subMLeave
AddHandler b2.MouseUp, AddressOf subMUp
End Sub

Private Sub subMUp(ByVal sender As Object, _
ByVal e As MouseEventArgs) Handles MyBase.MouseUp
RaiseEvent popupNotification()
End Sub

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

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

Private Sub formPopup_MouseLeave(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.MouseLeave
Dim cursorPosition As Point = PointToClient(Cursor.Position())
Dim rect As Rectangle = Me.ClientRectangle
If Not rect.Contains(cursorPosition) Then
RaiseEvent popupNotification()
End If
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
Const FORMWIDTH As Integer = 80
Const FORMHEIGHT As Integer = 80
f = New formPopup
f.Size = New Size(FORMWIDTH, FORMHEIGHT)
f.BackColor = Color.Yellow
f.FormBorderStyle = FormBorderStyle.FixedSingle
f.ControlBox = False
f.StartPosition = FormStartPosition.Manual
f.Location = New Point(Cursor.Position.X - FORMWIDTH \ 2, _
Cursor.Position.Y - FORMHEIGHT \ 2)
f.Show()
End Sub

Private Sub disposeOfPopupForm()
If Not Nothing Is f Then
f.Close()
f.Hide()
f = Nothing
End If
End Sub

Private Sub f_popupNotification() Handles f.popupNotification
Dim buttonPressed As Button = f.getButton
Call disposeOfPopupForm()
If Nothing Is buttonPressed Then
Debug.WriteLine("no button was pressed")
Else
Debug.WriteLine("Selected button: " + buttonPressed.Text)
End If
End Sub

End Class


"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
"Colin McGuire" <co***********@lycos.co.uk> schrieb
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


AFAIR I suggested to use

f.capture = true

instead of

button1.capture = true

in my last reply.
--
Armin

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

Nov 20 '05 #13

P: n/a
Brian - this isn't what I described or wanted. It is better!
Thank you very much.
Colin
"Brian" <no****@prairie.lakes.com> wrote in message news:<vr************@corp.supernews.com>...
OK, I think i got it (maybe).

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")
Dim pt As Point
Dim pt2 As Point
f = New formPopup()

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

f.StartPosition = FormStartPosition.Manual
f.Capture = True
f.Location = pt
f.Show()

Cursor.Position = f.PointToScreen(New Point(0, 0))

End Sub

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

End Sub
End Class

Public Class formPopup
Inherits System.Windows.Forms.Form

Dim rect As Rectangle
Dim rect1 As Rectangle

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)
Me.FormBorderStyle = FormBorderStyle.None

AddHandler Me.MouseUp, AddressOf fMouseUp
AddHandler b1.MouseEnter, AddressOf subMEnter
AddHandler b1.MouseLeave, AddressOf subMLeave
AddHandler b1.MouseUp, AddressOf subMUp
AddHandler Me.MouseMove, AddressOf fMouseMove

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

rect = New Rectangle(b1.Left, b1.Top, b1.Width, b1.Height)
rect1 = New Rectangle(b2.Left, b2.Top, b2.Width, b2.Height)

End Sub

Private Sub fMouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove

If rect.Contains(e.X, e.Y) OrElse rect1.Contains(e.X, e.Y) Then
If Not Me.Capture = False Then
Me.Capture = False
End If
ElseIf Me.Capture = False Then
Me.Capture = True
End If

End Sub

Private Sub fMouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp

Me.Capture = False
Me.Close()
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

Nov 20 '05 #14

P: n/a
Brian - this isn't what I described or wanted. It is better!
Thank you very much.
Colin
"Brian" <no****@prairie.lakes.com> wrote in message news:<vr************@corp.supernews.com>...
OK, I think i got it (maybe).

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")
Dim pt As Point
Dim pt2 As Point
f = New formPopup()

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

f.StartPosition = FormStartPosition.Manual
f.Capture = True
f.Location = pt
f.Show()

Cursor.Position = f.PointToScreen(New Point(0, 0))

End Sub

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

End Sub
End Class

Public Class formPopup
Inherits System.Windows.Forms.Form

Dim rect As Rectangle
Dim rect1 As Rectangle

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)
Me.FormBorderStyle = FormBorderStyle.None

AddHandler Me.MouseUp, AddressOf fMouseUp
AddHandler b1.MouseEnter, AddressOf subMEnter
AddHandler b1.MouseLeave, AddressOf subMLeave
AddHandler b1.MouseUp, AddressOf subMUp
AddHandler Me.MouseMove, AddressOf fMouseMove

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

rect = New Rectangle(b1.Left, b1.Top, b1.Width, b1.Height)
rect1 = New Rectangle(b2.Left, b2.Top, b2.Width, b2.Height)

End Sub

Private Sub fMouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove

If rect.Contains(e.X, e.Y) OrElse rect1.Contains(e.X, e.Y) Then
If Not Me.Capture = False Then
Me.Capture = False
End If
ElseIf Me.Capture = False Then
Me.Capture = True
End If

End Sub

Private Sub fMouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp

Me.Capture = False
Me.Close()
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

Nov 20 '05 #15

P: n/a
Comments inline. Thanks Brian, Hex, and Armin.
Colin

"hexathioorthooxalate" <ru***@REMOVESPAM.clara.co.uk> wrote in message news:<10***************@damia.uk.clara.net>...
Brian, I'm not sure your code does exactly what he wants. This is the
confusion I think: from his posting I believe he wants to "select" buttons
on the popup form while the mouse button is down, and further only display
the popup form while the mouse button is down. If the mouse button is ever
released, regardless of where it is, then the popup form is hidden/disposed
of.
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.


Colin, could you reply to confirm your exact desired behaviour please.

I did want to select buttons while the mouse button was down. I did
want the popup to disappear whenever the mouse button was released.
What Brian has written behaves more like a slick Windows programme and
this is what I will use. What Hex has written is what I asked for. I
asked for the wrong thing :(

Thank you both again
Colin
Nov 20 '05 #16

P: n/a
Comments inline. Thanks Brian, Hex, and Armin.
Colin

"hexathioorthooxalate" <ru***@REMOVESPAM.clara.co.uk> wrote in message news:<10***************@damia.uk.clara.net>...
Brian, I'm not sure your code does exactly what he wants. This is the
confusion I think: from his posting I believe he wants to "select" buttons
on the popup form while the mouse button is down, and further only display
the popup form while the mouse button is down. If the mouse button is ever
released, regardless of where it is, then the popup form is hidden/disposed
of.
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.


Colin, could you reply to confirm your exact desired behaviour please.

I did want to select buttons while the mouse button was down. I did
want the popup to disappear whenever the mouse button was released.
What Brian has written behaves more like a slick Windows programme and
this is what I will use. What Hex has written is what I asked for. I
asked for the wrong thing :(

Thank you both again
Colin
Nov 20 '05 #17

P: n/a
Colin,

I went and read your first post and finally figured out what you wanted
(should have done that in the first place). Place this into your form mouse
up event: It will handle the mouse up event as long as the mouse is within
the form. I haven't figured it out (yet) when the mouse is off the form. It
will get you 90% where you what to be, i think.
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp

If Not f Is Nothing Then Button1_MouseUp(sender, e)

End Sub

"Colin McGuire" <co***********@lycos.co.uk> wrote in message
news:ab**************************@posting.google.c om...
Comments inline. Thanks Brian, Hex, and Armin.
Colin

"hexathioorthooxalate" <ru***@REMOVESPAM.clara.co.uk> wrote in message

news:<10***************@damia.uk.clara.net>...
Brian, I'm not sure your code does exactly what he wants. This is the
confusion I think: from his posting I believe he wants to "select" buttons on the popup form while the mouse button is down, and further only display the popup form while the mouse button is down. If the mouse button is ever released, regardless of where it is, then the popup form is hidden/disposed of.
> 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.


Colin, could you reply to confirm your exact desired behaviour please.

I did want to select buttons while the mouse button was down. I did
want the popup to disappear whenever the mouse button was released.
What Brian has written behaves more like a slick Windows programme and
this is what I will use. What Hex has written is what I asked for. I
asked for the wrong thing :(

Thank you both again
Colin

Nov 20 '05 #18

P: n/a
Here's the final code. And it works!
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()

'-- Trap button1 messages
Button1.Capture = True

End Sub

Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
Debug.WriteLine("Button1 mouseup")

'-- Kill menu if it exists
If Not f Is Nothing Then
'-- Release capture
Button1.Capture = False
f.Hide()
f.Close()
f = Nothing
End If

End Sub
'-- Resets button1 capture back to true if mouse left the context menu
'-- without the user releasing the button
Private Sub Mouseleft() Handles f.MouseLeft
Button1.Capture = True
End Sub


Private Sub Button1_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove

'-- See if the user is over the context menu.
'-- If so, release button1 capture so the
'-- context menu can receive its events.
If Not f Is Nothing Then
Dim pt As New Point()
Dim pt2 As Point
pt2 = Cursor.Position()
pt = f.Location
Dim rect As New Rectangle(pt.X, pt.Y, f.Width, f.Height)
'-- If over the menu, release capture.
If rect.Contains(pt2.X, pt2.Y) Then
Button1.Capture = False
End If
End If

End Sub

End Class

Public Class formPopup
Inherits System.Windows.Forms.Form

Event MouseLeft()

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)
Me.FormBorderStyle = FormBorderStyle.None


AddHandler b1.MouseEnter, AddressOf subMEnter
AddHandler b1.MouseLeave, AddressOf subMLeave
AddHandler b1.MouseUp, AddressOf subMUp

'-- Add new handler for form mouse leave
AddHandler Me.MouseLeave, AddressOf subformLeave

'-- Optional, if you want to form to
'-- die when the user lifts the button up
AddHandler Me.MouseUp, AddressOf fMouseUp
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

'-- Raise a event if user didn't release button over the menu
'-- and they moved it back over the form or another window
Private Sub subformLeave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.MouseLeave
RaiseEvent MouseLeft()
End Sub

'****************************************
'-- Optional, if you want to form to
'-- die when the user lifts the button up
Private Sub fMouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp

'-- Close the form if that's what you want
Me.Close()
Debug.WriteLine("fMouseUp")

End Sub

'***************************************
Private Sub subMUp(ByVal sender As Object, ByVal e As MouseEventArgs)

'-- Do you want to do this whe an user lifts over a button? If not,
remove this.
Me.Close()

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


Nov 20 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.