CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Guest

    Right clicking in a Listbox

    Ok i cant figure this out. It seems like it should be a realitivly easy thing to do. Id like for when i right click in a listbox, the text that i right clicked on to become highlighted/selected. ive been working on it awhile and ive seen code to do it, but in my experiences with vb theres always more ways than one to write it. Ive searched through archives of vb code and never seen any examples. I think it would make a good addition also. Thanks in advance.


  2. #2
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: Right clicking in a Listbox

    You have to catch the Rt Mouse btn down/up and convert that coordinates to Listindex using a Listbox message and highlight that.

    This code works, i tested: Put a listbox (list1) an a form and copy the code and try

    Const TwoPower16 = 2 ^ 16 '!!. I need this for lparam to/from conv.
    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
    Const LB_ITEMFROMPOINT = &H1A9

    public Function MakeLParam(byval iHiWord as Integer, byval iLowWord as Integer) as Long
    MakeLParam = (iHiWord * TwoPower16) + iLowWord
    End Function

    private Sub List1_Mouseup(Button as Integer, Shift as Integer, X as Single, Y as Single)
    If Button = vbRightButton then
    Debug.print "Mouse up detected @("; X; ","; Y; ")"
    ' The X & Y will be in Twips. COnvert then to Pixels.
    Dim Xpixel as Integer, yPixel as Integer
    Dim lcurind as Long, lparam as Long
    on error GoTo ErrHell
    Xpixel = Int(X / Screen.TwipsPerPixelX)
    yPixel = Int(Y / Screen.TwipsPerPixelY)
    lparam = MakeLParam(yPixel, yPixel)
    lcurind = SendMessage(List1.hWnd, LB_ITEMFROMPOINT, byval 0, byval lparam)
    If (lcurind > &HFFFF&) then
    ' Clicked somewhere outside the allowed area, but inside the listbox!.
    ' What you want to do depends ...
    Debug.print "ItemFromPoint:"; lcurind
    else
    If List1.Style = 1 then
    List1.Selected(lcurind) = true
    else
    If List1.MultiSelect = 0 then
    List1.ListIndex = lcurind
    else
    List1.Selected(lcurind) = true
    End If
    End If
    End If
    End If
    Exit Sub
    ErrHell:
    Debug.print " Some error:"; Err.Number
    End Sub

    private Sub Form_Load()
    Dim ii
    ' put some items in list box
    for ii = 0 to 10
    List1.AddItem " Item - " & Str(ii)
    next ii
    End Sub








  3. #3
    Guest

    Re: Right clicking in a Listbox

    Wow thanks, thats alot. As i thought, there are more ways than one to do it. For the benifit of any other readers, here is what i got from planet source code from Cas21. Much thinks to the both of you.


    option Explicit
    private Const LB_GETTOPINDEX = &H18E

    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 Sub Form_Load()
    Dim i as Integer
    for i = 1 to 10
    List1.AddItem i
    next
    End Sub

    private Sub List1_MouseDown(Button as Integer, Shift as Integer, X as Single, Y as Single)
    Dim iRowHeight as Integer
    Dim iTop as Integer
    Dim iInsert as Integer
    If Button = 2 then
    iTop = SendMessage(List1.hwnd, LB_GETTOPINDEX, 0&, 0&)
    iRowHeight = TextHeight("x")
    iInsert = Y \ iRowHeight
    List1.ListIndex = iInsert + iTop
    End If
    End Sub





  4. #4
    Join Date
    May 1999
    Posts
    3,332

    Re: Right clicking in a Listbox

    works really well.
    Unfortunately, I have already reached my rating limit of 5 today. I will come back later.

    There is still one problem.
    I tried to copy and paste your code to a vb module and the result is a mess.
    I don't know if it's a problem with internet explorer or what, but I got the whole source text in one line.
    Ok, 5 minutes later I had a syntactically correct (but still messy-looking) code.
    "View source" doesn't help either, because then I get all the HTML tags in the code.

    Do you know a solution for easily copying published source code?
    at www.planet-source they offer a text box view. That makes it much easier.


  5. #5
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Right clicking in a Listbox

    Hi Lothar

    I have that problem copying from some sites too - although the [plug]VBCodeLibrary tool[/plug] ;-) will handle the formatting fine - you can then just copy it back out again.


    Chris Eastwood

    CodeGuru - the website for developers
    http://www.codeguru.com/vb

  6. #6
    Join Date
    May 1999
    Posts
    3,332

    Re: Right clicking in a Listbox

    Hi Chris,

    that may be true, but the VB CodeLibrary has become ShareWare AFAIK.
    I'd have a very hard time to convince my boss to spend money on such a tool.


  7. #7
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Right clicking in a Listbox

    You could always :

    1. Just keep on using it and ignore the Nag-Screen

    or

    2. Just copy the code from a web-page into any program that uses an RTF box (eg. WordPad) - you lose the formatting/indenting, but you've got a code formatting Add-In haven't you ? I've got the Source-Code for one somewhere if you're interested.


    Chris Eastwood

    CodeGuru - the website for developers
    http://www.codeguru.com/vb

  8. #8
    Join Date
    May 1999
    Posts
    3,332

    Re: Right clicking in a Listbox

    That's great! I didn't know about the RTF trick!

    And yes, I am interested in that Add-in you mentioned, how could we transfer that. I think that could be a useful code snippet for other developers, too.


  9. #9
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Right clicking in a Listbox

    I downloaded it off the Net about a year ago (?) - I've been using it ever since so I'll try and find the original URL to post here (and on the CodeGuru site).



    Chris Eastwood

    CodeGuru - the website for developers
    http://www.codeguru.com/vb

  10. #10
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Right clicking in a Listbox

    Just found it :

    http://www.bmsltd.co.uk/Excel/Default.htm

    -look a bit further down the page and you'll see it.

    Chris Eastwood

    CodeGuru - the website for developers
    http://www.codeguru.com/vb

  11. #11
    Guest

    Re: Right clicking in a Listbox

    you mean the Indenter.zip, right?
    It's Excel stuff.
    Well, that's a really long way to go: copy and paste it to Wordpad than start Excel...

    I have seen an add-in on planet-source that allows you to pick source code snippets from planet-source without ever leaving VB.
    That would be cool for the codeguru site.


  12. #12
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Right clicking in a Listbox

    That's the one - it started off as an Excel add-in but the guy converted it for VB5 about a year ago.

    I've just revisited his site and he's got another new version for VB5/6 with several bug fixes and new options - a great add-in.



    Chris Eastwood

    CodeGuru - the website for developers
    http://www.codeguru.com/vb

  13. #13
    Guest

    Re: Right clicking in a Listbox

    that's funny, I downloaded it too and found only 2 files: xla and xls.
    I'll check again.

    Why the hell do I show up as "anonymous"? and why are my font settings lost again?
    I get tons of errors when trying to access the codeguru site...
    (I guess, that's a different story)


  14. #14
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Right clicking in a Listbox

    Hi Lothar

    I'm getting the same problem with Anonymous (if you keep hitting 'refresh' it seems to work) - I'll drop Zafir a line and see if he's aware of any problems (it's 3am at the moment over there so it'll take some time to investigate).


    The link to the actual download is : http://www.bmsltd.co.uk/DLCount/DLCo...ndenterVB5.exe


    Chris Eastwood

    CodeGuru - the website for developers
    http://www.codeguru.com/vb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured