devxlogo

Make Windowless, Transparent UserControls Clickable

Make Windowless, Transparent UserControls Clickable

Microsoft has documented a problem with windowless User-Controls that have a transparent Backstyle. Once a form contains such a control, you can’t select it by clicking on it with the mouse; this makes it tough to move the control. (See Microsoft Knowledge Base article Q188234 for details.) Use this code workaround that allows you to click on and move these controls at design time. First, it uses the HitTest event to make the control always act as if it is clicked, regardless of mouse coordinates. This usage causes the UserControl_Click event to fire, which the owner can observe through the raised Click event:

 Private Sub UserControl_HitTest(X As Single, Y _	As Single, HitResult As Integer)	' Always act as if the control was hit	If HitResult = vbHitResultOutside Then		HitResult = vbHitResultHit	End IfEnd SubPublic Event Click()Private Sub UserControl_Click()	' Let the form handle the click	RaiseEvent ClickEnd Sub

In production code, if only portions of the control should be clickable in run mode, test for design mode vs. run mode in the HitTest event. Use this method only in design mode, and your own custom test in run mode.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist