|
|
|
|
Ask the MultiValued Visual Basic Expert - #10(as published in Spectrum magazine)updated September 9, 1998To email your questions to "Ask the MultiValued VB
Expert", click here.
|
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 dont really get your list box. Can you turn off this behavior? Yes you can, but Ill 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 dont want it to show at the top of your form, set the menu entrys 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. Heres the code:
Sub Text1_MouseDown (Button As Integer, ...)
....If Button = vbRightButton Then
........Text1.Enabled = False
........PopupMenu MenuName1
........Text1.Enabled = True
....End If
End Sub
![]()
|
Copyright © 2006 intellact
|