?
Solved

Right Click Selects item in list box

Posted on 2004-08-25
4
Medium Priority
?
345 Views
Last Modified: 2006-11-17
I have a list box and when I right click I have a menu pop up. What I want to happen is the item under the mouse pointer to become selected. Any suggestions?
0
Comment
Question by:DARKSAGES
  • 2
  • 2
4 Comments
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 11899580
What Language????

Trap MouseDown event and if you are using VB.NET use

ListBox1.SelectedIndex = ListBox1.IndexFromPoint(e.X, e.Y)

you can convert the statement ot C#, in case you are using VB you might have to use API to find out on which item of the list the mouse was right clicked.
0
 

Author Comment

by:DARKSAGES
ID: 11899792
vb6
0
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 2000 total points
ID: 11900308
Declare the APIs at form level

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 Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As _
    Any, dest As Any, ByVal numBytes As Long)
Const LB_ITEMFROMPOINT = &H1A9


Add a new function

Function GetListBoxItemFromPoint(lb As ListBox, ByVal X As Long, _
    ByVal Y As Long) As Long
    Dim lParam As Long

   lParam = X
   CopyMemory ByVal VarPtr(lParam) + 2, Y, 2

    GetListBoxItemFromPoint= SendMessage(lb.hwnd, LB_ITEMFROMPOINT, 0, _
        ByVal lParam)
End Function

Now Use this function From MouseDown event of the list box

Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then
    Dim i As Integer
    i = GetListBoxItemFromPoint(List1, ScaleX(X, vbTwips, vbPixels), ScaleY(Y, vbTwips, vbPixels))
    List1.Selected(i) = True
End If
End Sub

0
 

Author Comment

by:DARKSAGES
ID: 11908497
Thanx it works great.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Starting up a Project

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question