right click to select listbox item...

D

Dan Bass

If you look at explorer, right clicking on a file, first selects the file,
then throws up the context menu relating to that selection.

With a Windows ListBox control and a simple context menu, the default
behaviour seems to display the context menu on the listbox, but there is no
selection.

Now I know that explorer's using a listview, and that the context menu acts
differently if you click on open space (no selection), but can I simulate a
right click selection in a listbox?
Is my approach wrong in what I'm trying to achieve?
Is what I'm trying to acheive even possible?

Thanks.

Daniel.
 
N

Nicholas Paldino [.NET/C# MVP]

Dan,

It should be relatively simple. Basically, you want to detect when the
mouse button is pushed down (not released). Create an event handler for the
mouse down event on the listbox. Then, you can pass the coordinates of the
mouse down button (assuming it is the right click button) and pass it to the
IndexFromPoint method on the listbox, which will return the index of the
item at that point. From there, you can select that item.

Hope this helps.
 
D

Dan Bass

Nicholas,

Thanks, I'll try that out! =o)

Dan.

Nicholas Paldino said:
Dan,

It should be relatively simple. Basically, you want to detect when the
mouse button is pushed down (not released). Create an event handler for
the mouse down event on the listbox. Then, you can pass the coordinates
of the mouse down button (assuming it is the right click button) and pass
it to the IndexFromPoint method on the listbox, which will return the
index of the item at that point. From there, you can select that item.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Dan Bass said:
If you look at explorer, right clicking on a file, first selects the
file, then throws up the context menu relating to that selection.

With a Windows ListBox control and a simple context menu, the default
behaviour seems to display the context menu on the listbox, but there is
no selection.

Now I know that explorer's using a listview, and that the context menu
acts differently if you click on open space (no selection), but can I
simulate a right click selection in a listbox?
Is my approach wrong in what I'm trying to achieve?
Is what I'm trying to acheive even possible?

Thanks.

Daniel.
 
D

Dan Bass

It sort of works... When the click occurs, the popup occurs, the selection
is made. But because the context menu is active, the list box doesn't look
to have changed focus. Then when you leave the context menu, the selection
is seen.

Any ideas?

my code for the mouse up is as follows.

private void listBox1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
int indexover = listBox1.IndexFromPoint ( e.X, e.Y );
if ( indexover >= 0 && indexover < listBox1.Items.Count )
{
listBox1.SelectedIndex = indexover;
}
listBox1.Refresh();
}
 
N

Nicholas Paldino [.NET/C# MVP]

Dan,

You did it in the mouse up event. You should do it in the mouse down
event, as this mirrors the functionality in explorer. If you right-click in
explorer, and hold the button down, you will notice that the selection is
made, but the context menu does not pop up. Only when you release the
button does the context menu show up.

Also, do you need to make a call to Refresh? I don't think you do.

Finally, make sure you check to see that the right mouse button is
clicked.
 
D

Dan Bass

Yep that does the trick, thanks again for your help.

Happy New Year!


Nicholas Paldino said:
Dan,

You did it in the mouse up event. You should do it in the mouse down
event, as this mirrors the functionality in explorer. If you right-click
in explorer, and hold the button down, you will notice that the selection
is made, but the context menu does not pop up. Only when you release the
button does the context menu show up.

Also, do you need to make a call to Refresh? I don't think you do.

Finally, make sure you check to see that the right mouse button is
clicked.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dan Bass said:
It sort of works... When the click occurs, the popup occurs, the
selection is made. But because the context menu is active, the list box
doesn't look to have changed focus. Then when you leave the context menu,
the selection is seen.

Any ideas?

my code for the mouse up is as follows.

private void listBox1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
int indexover = listBox1.IndexFromPoint ( e.X, e.Y );
if ( indexover >= 0 && indexover < listBox1.Items.Count )
{
listBox1.SelectedIndex = indexover;
}
listBox1.Refresh();
}
 
H

Herfried K. Wagner [MVP]

Dan Bass said:
If you look at explorer, right clicking on a file, first selects the file,
then throws up the context menu relating to that selection.

With a Windows ListBox control and a simple context menu, the default
behaviour seems to display the context menu on the listbox, but there is
no selection.

\\\
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

' Show context menu here using 'ContextMenu.Show'...
End If
End If
End Sub
///
 

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

Top