Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Aug 6, 1999

Emulate a Click Event for Right-Clicks Over Commandbutton Controls

Sometimes it's useful to trap the right click over controls such as CommandButton. Unfortunately, the Click event only fires for left button clicks. The MouseDown and MouseUp events fire for both buttons, and even report which button was clicked, but nothing in life could ever be that simple, right?

The first problem is that if you use the MouseDown event, you trap the click when the user clicks down on the key, which is counter to the way things normally work in Windows. For instance, when you click the left button, the control's Click event won't fire until you release, giving you the ability to slide off the control before releasing. A fire-on-downstroke event gives no such safety net.

So you're probably thinking, "Well, then just use the MouseUp event!" This solution creates another gotcha: Even if you slide off the control before releasing, the MouseUp event fires anyway!

Fortunately, the MouseUp event reports the mouse cursor's X and Y positions, and using simple math comparing them to the control's placement and size, it's easy to determine whether the mouse was over the control at the instant it was released. Here's how you do it:

 
Option Explicit
Private Sub Command1_MouseUp(Button As Integer, _
	Shift As Integer, X As Single, Y As Single)
Dim OffMe As Boolean
		If Button = 2 Then	'right button
			X = X + Command1.Left
			Y = Y + Command1.Top
			OffMe = False
			Select Case X
				Case Is < Command1.Left, Is > _
				(Command1.Left + Command1.Width)
					OffMe = True
		End Select
		Select Case Y
			Case Is < Command1.Top, Is > _
				(Command1.Top + Command1.Height)
					OffMe = True
		End Select
		If Not OffMe Then
			'*****************
			'Your code goes here
			'*****************
		End If
	End If
End Sub

Ron Schwarz
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date