Ask the MultiValued Visual Basic Expert - #10

(as published in Spectrum magazine)

updated September 9, 1998
To email your questions to "Ask the MultiValued VB Expert", click here.
Copyright © 1996-98 Caduceus Consulting. All rights reserved.

Popup Menus from a Right Mouse Click

I caught your class at Spectrum in March and thought it was terrific!! I have been looking through my help documentation and books at hand and could not find an answer to this problem. You said that if we had a question, you would take a stab at it, so here goes.

I would like to make a list box visible when the user clicks the right button on a certain text box, but have been having trouble with the default (Undo, Cut, Copy, Paste, Delete, Select all) window popping up before or after my list box appears depending on whether I put the code in the mouse down or mouse up event.

Do you know of a way to disable this or get around it? Any help would be much appreciated. - Steve Shaffer

You are absolutely correct, Steve. If any reader has a question on Visual Basic, especially if it is related to MultiValue work (but not necessarily), I will answer it or track down the information. This one is actually quite simple, but there’s a trick involved. I dare you other readers to come up with something a little more challenging!

I presume the idea is that you want to give additional help or list options for a user when they are in a particular text box. There are actually three ways of creating functionality similar to what you want. I leave it to each individual reader to determine what solution best fits their needs. Before I tell you about that trick, let’s consider all three possibilities:

  1. A combo box.
  2. A popup list box.
  3. A windows-style popup menu.

The Combo Box approach

If you want to give the user predefined options for the actual contents of the text box, then a combo box is probably an excellent choice. A combo box is a combination of a text box and a list box – thus the name. It is also sometimes referred to as a dropdown list box. You would use it instead of the text box that you have now. It has a Text property, like a text box, but it also has List and ListIndex properties that you can use to create the dropdown list and select from it. The behavior of a list box is determined by its Style property. There are three styles:

Combo Box Style

Behavior

0 – Dropdown combo (Default). Includes a drop-down list and a text box. The user can select from the list or type in the text box.
1 – Simple Combo Includes a text box and a list, which doesn't drop down. The user can select from the list or type in the text box. The size of a Simple combo box includes both the edit and list portions. By default, a Simple combo box is sized so that none of the list is displayed. Increase the Height property to display more of the list.
2 – Dropdown List This style only allows selection from the drop-down list.

Styles 0 and 2 are the most common. If the user can type free text and ignore the list, choose 0. If they must select from the list, choose 2. For a combo box that looks up entries as the user types, see my May/June 97 column. Also be careful where you put your code: even though clicking on a combo box list entry changes its Text property, you do not get a Change event (see Jan/Feb 98 column).

The Popup List Box

Making a list box appear when you right click on a text box is not difficult to do, [as you demonstrated in the sample code that you attached to your question]. Simply create a list box on the same form, set its Visible property to False, and populate it with whatever you want in the list. The MouseUp event will tell you which mouse button was released, so you can popup your list box when the user right clicks:

Sub Text1_MouseUp (Button As Integer, ...)
....If Button = vbRightButton Then
........List1.Visible = True
....End If
End Sub

The catch that you hit is that in Windows 95 and higher, Windows provides its own little default edit popup menu when you right click on a text box, so you don’t really get your list box. Can you turn off this behavior? Yes you can, but I’ll maintain the suspense until we review the third option.

The Windows-style Popup Menu

Visual Basic actually gives you an elegant way of popping up a menu just the way that Windows 95 does when you right click anywhere. Simply create a menu entry with one or more submenu entries, using the Visual Basic built-in menu editor. If you don’t want it to show at the top of your form, set the menu entry’s Visible property to False. You can then use the PopupMenu method in your MouseUp event to display the menu right where your mouse is. But you still have to disable Windows own default popup menu for text boxes. The trick is to temporarily disable the control! Problem solved. Here’s the code:

Sub Text1_MouseDown (Button As Integer, ...)
....If Button = vbRightButton Then
........Text1.Enabled = False
........PopupMenu MenuName1
........Text1.Enabled = True
....End If
End Sub

To email your questions to "Ask the MultiValued VB Expert", click here.
Copyright © 1998 Caduceus Consulting. All rights reserved.
Updated: September 9, 1998.

Return to Caduceus Consulting Home Page

Copyright © 2006 intellact
Last modified: Thursday May 25, 2006