ListBox

  • Thread starter Thread starter Dmitry Karneyev
  • Start date Start date
D

Dmitry Karneyev

Hi!

Could anyone tell me how to select listbox items by right button of the
mouse?
It works originally only with left button.

Thanks.
 
You can use the MouseDown event and check if the button was the right
mouse button

private void listBox1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if(e.Button == MouseButtons.Right)
{
// right mouse button was clicked
}
}

You probably have to find out which item was "selected".
e.X and e.Y contains the coordinates of the click.
 
Thanks for reply Morten !

I've been looking for a little bit simplier solution.

I need such thing to use in the next situation:
user clicks the item and wants to view a context menu, but menu shows
for selected item which is not always the item user clicks by right button
of the mouse.

P.S. retieving wich item was selected by using mouse coordinates looks for
me very hard.

"Morten Wennevik" <[email protected]> ???????/???????? ? ????????
?????????: You can use the MouseDown event and check if the button was the right
mouse button

private void listBox1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if(e.Button == MouseButtons.Right)
{
// right mouse button was clicked
}
}

You probably have to find out which item was "selected".
e.X and e.Y contains the coordinates of the click.
 
Hi,

I think it's not THAT difficult, take a look at ListBox.GetItemRectangle()
it may be a little performance poor as you have to iterate in the list but I
think that it can be done

Hope this help,
 
There is no other way. You have to analyze where right-click was done.

And it's not really hard. Take a look at IndexFromPoint method.

HTH
Alex
 
I think about next variant:
maybe it is possible to intercept mouse clicks events and to redirect them
with other properties.
I mean intercept button clicks analyze which button was clicked and if it
was right button - raise 2 events:
1. all properties the same without "button number"
2. the same event to display context menu.

What do you think guys about it?

AlexS said:
There is no other way. You have to analyze where right-click was done.

And it's not really hard. Take a look at IndexFromPoint method.

HTH
Alex
 
Well, actually your variant is rather good!
I'll use it.

Thanks.

AlexS said:
There is no other way. You have to analyze where right-click was done.

And it's not really hard. Take a look at IndexFromPoint method.

HTH
Alex
 
If I understand you correct you can do that by adding common stuff before
checking for mouse button.

private void listBox1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
// do common stuff

if(e.Button == MouseButtons.Right)
{
// show context menu
}
}
 
Back
Top