Solved

Right Click Selects item in list box

Posted on 2004-08-25
4
334 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

A short article about a problem I had getting the GPS LocationListener working.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Starting up a Project

696 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