Select item from list box with right mouse click

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

My project has a form, two list boxes (lstArchive and lstSchedule) and two
buttons (btnSendArchive and btnSendSchedule)

Each list box represents the content of a directory. The command buttons
move the selected item out of one directory and into the other and refreshes
the list boxes to reflect the change in directory contents

I now want to add "context menu" functionality to the list boxes to preform
the same function as the two buttons. My problem is I can right click on the
list box and send the selected item to the other directory with the chosen
menu item ..... but right-clicking does not itself select the item over which
the mouse is hovering. In other words I have to left-click to select an item
before I can right-click to activate the contect menu for the selected item.

Is there any way that right-clicking in a list box can be used to select an
item in that box.

Any help would be appreciated

Regards

Michael Bond
 
Hi, this should work

Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) _ Handles ListBox1.MouseDown
If e.Button = MouseButtons.Right Then
ListBox1.SelectedIndex = ListBox1.IndexFromPoint(e.X, e.Y)
End If
End Sub

Hth Greetz Peter
 
mabond wrote:
....
I now want to add "context menu" functionality to the list boxes to preform
the same function as the two buttons. My problem is I can right click on the
list box and send the selected item to the other directory with the chosen
menu item ..... but right-clicking does not itself select the item over which
the mouse is hovering. In other words I have to left-click to select an item
before I can right-click to activate the contect menu for the selected item.

Is there any way that right-clicking in a list box can be used to select an
item in that box.

Private Sub ListBox1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles ListBox1.MouseDown
If e.Button = MouseButtons.Right Then
Dim index As Integer = ListBox1.IndexFromPoint(New
Point(e.X, e.Y))
If index >= 0 Then
ListBox1.SelectedItem = ListBox1.Items(index)
End If
End If
End Sub

Because your context menu code fires on MouseUp (right?) this should
slot in just fine.
 
Peter, Larry

Thanks both .... especially for the speed of response.

Appreciate the help

Regards

Michael
 
mabond said:
Is there any way that right-clicking in a list box can be used to select
an
item in that box.

\\\
Private Sub ListBox1_MouseUp( _
ByVal sender As Object, _
ByVal e As MouseEventArgs _
) Handles ListBox1.MouseUp
If e.Button = MouseButtons.Right Then
Dim n As Integer = Me.ListBox1.IndexFromPoint(e.X, e.Y)
If n <> ListBox.NoMatches Then
Me.ListBox1.SelectedIndex = n
End If
End If
End Sub
///
 
Herfried said:
Dim n As Integer = Me.ListBox1.IndexFromPoint(e.X, e.Y)
If n <> ListBox.NoMatches Then

Use of a meaningful named constant, now that's classy :)
Me.ListBox1.SelectedIndex = n

I can't believe I set SelectedItem to Items(n) :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top