|
-
Oct 7th, 2006, 05:24 PM
#1
Thread Starter
Junior Member
Selecting an item in a listbox
Hi All,
I have a listbox with a popup menu when I right-click on one of the names in the list. This all works fine, but in order to make this work properly I have to first left click on the name I want to select, then I can right click to bring up the popup menu.
I'm wondering if there is a way I can just right click directly onto my chosen name in the listbox without first having to left click to select it?
Anyone have any ideas?
Thanks,
-
Oct 7th, 2006, 05:31 PM
#2
Re: Selecting an item in a listbox
I don't think that's possible. ListBox will change .ListIndex property ONLY on the left click. Therefore, .ListIndex won't change on the right click, so you really don't have anything to select.
-
Oct 7th, 2006, 05:38 PM
#3
Thread Starter
Junior Member
Re: Selecting an item in a listbox
ok, thanks. I guess I'll just change it so the menu pops up when I left click on it.
-
Oct 7th, 2006, 05:42 PM
#4
Re: Selecting an item in a listbox
No, it can be done with right click too. I forgot the way, search here, you would find a way.
-
Oct 7th, 2006, 05:43 PM
#5
Re: Selecting an item in a listbox
here's a quick example of how you can get the Index of the item that was clicked on without it being selected. It's rough and ready but you should be able to fit it to your needs fairly simply:
VB Code:
Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lCurrentPos As Long
Set Me.Font = List1.Font
lCurrentPos = List1.TopIndex + Y \ Me.TextHeight("A")
Debug.Print lCurrentPos
End Sub
-
Oct 7th, 2006, 05:46 PM
#6
Re: Selecting an item in a listbox
Live and learn
-
Oct 7th, 2006, 05:55 PM
#7
Re: Selecting an item in a listbox
This also works.
VB Code:
Option Explicit
Private Const LB_ITEMFROMPOINT As Long = &H1A9
Private Declare Function SendMessage Lib "User32.dll" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Private Sub List1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim lngRetValue As Long
Dim lngIndex As Long
Dim lngCurPos As Long
On Error Resume Next
If Button = vbRightButton Then
lngCurPos = (ScaleX(x, vbTwips, vbPixels)) Or (ScaleY(y, vbTwips, vbPixels) * &H10000)
lngRetValue = SendMessage(List1.hwnd, LB_ITEMFROMPOINT, 0&, ByVal lngCurPos)
lngIndex = lngRetValue And &HFFFF
List1.ListIndex = lngIndex
MsgBox List1.List(List1.ListIndex)
End If
If Button = vbRightButton Then
PopupMenu mnuEdit
End If
End Sub
-
Oct 7th, 2006, 05:59 PM
#8
Re: Selecting an item in a listbox
This should work..
VB Code:
Private Const LB_ITEMFROMPOINT = &H1A9
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Sub Form_Load()
List1.AddItem "item 1"
List1.AddItem "item 1"
List1.AddItem "item 1"
List1.AddItem "item 1"
List1.AddItem "item 1"
End Sub
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim Index As Long
Dim Coord As Long
If Button = vbRightButton Then
Coord = ((Y / Screen.TwipsPerPixelY) * &H10000) + (X / Screen.TwipsPerPixelX)
Index = SendMessage(List1.hwnd, LB_ITEMFROMPOINT, 0&, ByVal lCoord)
If Index >= 0 Then
List1.ListIndex = Index
MsgBox "You right clicked on " & List1.Text
End If
End If
End Sub
Edit: Late!!
-
Oct 7th, 2006, 06:03 PM
#9
Thread Starter
Junior Member
Re: Selecting an item in a listbox
Thanks everyone, seems it can be done... ;-)
-
Oct 7th, 2006, 06:14 PM
#10
Re: Selecting an item in a listbox
Hey Marty, could you please tell me, why do we multiply &H10000 or 65536 to the Y pixelated value.
-
Oct 7th, 2006, 06:25 PM
#11
Re: Selecting an item in a listbox
Actually I have no idea, it's just a sample that I picked up but unfortunately I didn't record where (probably here) and who I got it from.
-
Oct 7th, 2006, 07:37 PM
#12
Re: Selecting an item in a listbox
 Originally Posted by MartinLiss
Actually I have no idea, it's just a sample that I picked up but unfortunately I didn't record where (probably here) and who I got it from.
OK, got it. the lparam needs to be divided into High- and Low-order words, here Y coord is a High order word while X coord is a low order word.
Details, about High - Low order words
A Long (Long Integer) takes up 32 bits of memory in VB and An word ("integer" in VB6) takes up 16 bits.
Two words can be used instead of one Long integer.
Some Windows API pass two word parameters to a function that normally takes one long integer.
To seperate this long integer into two words you take the first 16 bits into one word (the "high word") and the second 16 bits into the other word (the "low word"). So when we multiple &H10000 or 65536, it makes the resultant value High-order word
Here, Y coord plays vital role in selecting a value, therefore it has been made HO value.
-
Oct 7th, 2006, 08:37 PM
#13
Re: Selecting an item in a listbox
 Originally Posted by MartinLiss
Actually I have no idea, it's just a sample that I picked up but unfortunately I didn't record where (probably here) and who I got it from.
I did a little research and the credit should go to RhinoBull. See post #2 in this thread.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|