Solved

Right Click Selects item in list box

Posted on 2004-08-25
4
327 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

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