How to place a popup menu directly below a button in VB6

I have a VB6 application. There is a basic form. It contains a user control. This user control has a control button. When I press the button, I want the menu to appear, so the top left of the menu is directly below the bottom left of the button.

if:

frm.PopupMenu mnuBlah 

a menu appears where the mouse is located.

if I try to provide coordinates

 frm.PopupMenu mnuBlah, btn.Left, btn.Top + btn.Height 

the math comes out completely wrong, and the menu is displayed away from the goal.

Iโ€™m not quite sure what types of coordinates the PopupMenu call requires and how to calculate the position under the button.

+4
source share
3 answers

PopupMenu wants the X and Y coordinates in the ScaleMode of the form on which the menu is called. In this case, this probably means a user control.

But it really depends on where you are working with the MouseDown event. (You do not receive coordinates with the Click event.) If your event handler is in the user control itself, then you should not have problems using the supplied coordinates. However, the code you posted doesn't have the Flags parameter (should be vbPopupMenuLeftAlign), which might explain why your coordinate math doesn't work. In addition, you must define the menu as a property of the user control itself.

On the other hand, if you trigger the MouseDown event from a user control and process it in the contained form, the pop-up menu should be a property of the contained form. In this case, you can ignore the X and Y parameters, but you must do the coordination calculations yourself. The left edge of the button relative to the main form will be the sum of the left edge of the userโ€™s control and the offset of the button inside the control. Similar math will work to calculate the bottom edge of the button. In other words:

 Private Sub UserControl1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) PopupMenu mnuPopup, vbPopupMenuLeftAlign, _ UserControl1.Left + UserControl1.Button.Left, _ UserControl1.Top + UserControl1.Button.Top + UserControl1.Button.Height End Sub 

Note that this will require that you also open the (Button) as a property of the user control so that you can access its Left, Top, and Height properties. This is easy enough:

 Public Property Get Button() As CommandButton Set Button = Command1 End Property 

Raising an event is quite simple; just put

 Public Event MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) 

at the top of the user control, then use this:

 RaiseEvent MouseDown(Button, Shift, X, Y) 

in the MouseDown event of a button in a user control.

Finally, to attach an event handler in the main form, use the two drop-down lists at the top of the code editing window. Select a user control from the list on the left, then select the MouseDown event from the list on the right. This inserts an event handler that you populate with the code above.

How much better, processing the menu inside the user control itself and raising significant events when choosing one of them separates things. The user control processes the user interface, leaving the main application to process everything that needs to be done in response to the user's choice, without worrying about the layout of the menu.

This way you will not actually raise a MouseDown event; rather, you will raise events like OptionOneSelected, OptionTwoSelected, or OptionThreeSelected. (Of course, more meaningful names would be much better.)

In any case, I hope it is not too late to be of service ...

+2
source

You need to specify the height and left values โ€‹โ€‹for the parent container (usually this is a form, but it can also be a frame or another container).

I think you are looking for something like this:

 btn.Parent.PopupMenu mnuBlah, vbPopupMenuLeftAlign , btn.Left, btn.Top + btn.Height 
+1
source

You need to pass the vbPopupMenuLeftAlign flag to the PopupMenu method. Note that this contradicts the documentation on the PopupMenu method (can Microsoft really make a mistake?), But it works.

 frm.PopupMenu mnuBlah, vbPopupMenuLeftAlign, btn.Left, btn.Top + btn.Height 
+1
source

All Articles