Solved

Right Click Selects item in list box

Posted on 2004-08-25
4
323 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 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Title # Comments Views Activity
topping2 challenge 13 94
Base1 Encode/Decode 3 81
Please help| Getting the syntax error in below query. 2 65
Programming Codes 2 21
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.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

860 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