Results 1 to 4 of 4

Thread: [RESOLVED] [2005] ListBox RightClick Selects

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    108

    Resolved [RESOLVED] [2005] ListBox RightClick Selects

    Hey there

    I'm using this code to select an item in a ListBox when a user right clicks on it
    VB Code:
    1. Private Sub listOnline_MouseDown(ByVal sender As System.Object, _
    2. ByVal e As System.Windows.Forms.MouseEventArgs) Handles listOnline.MouseDown
    3.             If e.Button = Windows.Forms.MouseButtons.Right Then
    4.                 Dim mouseY As Integer = listOnline.PointToClient(Windows.Forms.Cursor.Position).Y
    5.                 Dim itemIndex As Integer = (mouseY \ listOnline.ItemHeight) - listOnline.TopIndex
    6.                 listOnline.SelectedIndex = itemIndex
    7.  
    8.                 listOnline.ContextMenuStrip = blockMenu
    9.                 Dim myString As String = listOnline.SelectedItem
    10.                 Dim mySplit(2) As String
    11.                 mySplit = Split(myString, " - ")
    12.                 If InStr(mySplit(0), "¤") Then
    13.                     Block.Text = "Unblock: " & mySplit(2)
    14.                 Else
    15.                     Block.Text = "Block: " & mySplit(2)
    16.                 End If
    17.             End If
    18.     End Sub
    It works great....but....my list is bigger than the container so a scrollbar appears, I can only rightclick select things that appear in the ListBox when the scrollbar is all the way up, if I scroll down and try to rightclick select something it wont work. It basically selects the "wrong" item....the item that would be in that place if the scrollbar was all the way up....

    It probably has something to do with this:
    VB Code:
    1. Dim itemIndex As Integer = (mouseY \ listOnline.ItemHeight) - listOnline.TopIndex
    .....what could I use instead to make it work?
    VB.Net 2005 Express
    .Net Framework 2.0

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: [2005] ListBox RightClick Selects

    Use this...
    Code:
    ListBox1.IndexFromPoint(New Point(e.X, e.Y))
    in your MouseDown handler to get the correct item that you are over.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] ListBox RightClick Selects

    You should use the IndexFromPoint method to determine the index of the item at the point that was clicked. That will work regardless of whether or how far the control is scrolled. Here's a quote from the help topic:
    You can use this method to determine which item within the list is selected when a user right-clicks over the ListBox.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] ListBox RightClick Selects

    I need to consume more caffeine it would appear.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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