Solved

Right Click Selects item in list box

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
find a node in VST 2 47
Fibonacci challenge 11 84
stringclean challenge 26 58
Not needed 13 58
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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 …
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 …

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now