Emulate a Click Event for Right-Clicks Over Commandbutton Controls

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 ExplicitPrivate 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 IfEnd Sub
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes