Library tutorials & articles
ListBox Control
- Introduction
- Styles
- Properties and Events
- Adding Items
- Removing Items
- Getting the selected items
- Using the CheckListBox
- Select an item using the right mouse button
Select an item using the right mouse button
Thanks to Lada Simicek for this (and many other) tips.
This tip shows you how you can select an item in the listbox using right mouse button. This is useful when you want to show popup menu, for example. This tip doesn't uses any API functions. All the code does is calculates the index of the item which you has clicked on from mouse's co-ordinates. For this code to work, it needs to know whether the listbox's property Style is set on Standard or CheckBox. Furthermore it is necessary to set the form's property FontSize on the same as ListBox (this is caused because the property TextHeight is only used in Form control.
Here is the short example that shows you how you can correctly select an item in the listbox via right mouse button:
Please, insert on the Form1 one listbox named List1. Try changing its style properties too. (Please note that this code does not appear to work on some systems. If anyone works out why, please email me!)
Private Const STANDARD_LISTBOX = 1
Private Const CHECKBOX_LISTBOX = 1.05
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 250
List1.AddItem "Item No."
& i
Next i
End Sub
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As
Single, Y As Single)
Dim intIndex As Integer
Dim dblStyle As Double
Select Case List1.Style
Case 0 'Standart
dblStyle = STANDART_LISTBOX
Case 1 'CheckBox
dblStyle = CHECKBOX_LISTBOX
End Select
If Button = vbRightButton Then
With List1
Picture1.FontSize
= List1.FontSize
intIndex
= ((Y / Screen.TwipsPerPixelY) _
((dblStyle
* (Picture1.TextHeight("A"))) _
/ Screen.TwipsPerPixelY))
+ .TopIndex
If intIndex
> .ListCount - 1 Then intIndex = -1
.ListIndex
= intIndex
.SetFocus
End With
End If
End Sub
Related articles
Related discussion
-
Run-time error '91'
by converter2009 (1 replies)
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
Suppose I have numeric values in my listbox. How do I add them and display the result on a message box.
This thread is for discussions of ListBox Control.