By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,908 Members | 2,055 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,908 IT Pros & Developers. It's quick & easy.

ListBox

P: n/a
Hi!

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

Thanks.
Nov 15 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #2

P: n/a
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" <Mo************@hotmail.com> ???????/???????? ? ????????
?????????: news:oprxjnygnahntkfz@localhost...
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.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #3

P: n/a
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

"Dmitry Karneyev" <ka******@msn.com> wrote in message
news:uf**************@TK2MSFTNGP10.phx.gbl...
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" <Mo************@hotmail.com> ???????/???????? ? ????????
?????????: news:oprxjnygnahntkfz@localhost...
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.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

Nov 15 '05 #4

P: n/a
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Dmitry Karneyev" <ka******@msn.com> wrote in message
news:uf**************@TK2MSFTNGP10.phx.gbl...
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" <Mo************@hotmail.com> ???????/???????? ? ????????
?????????: news:oprxjnygnahntkfz@localhost...
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.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

Nov 15 '05 #5

P: n/a
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" <sa***********@SPAMsympaticoPLEASE.ca> сообщил/сообщила в новостях
следующее: news:ev**************@TK2MSFTNGP09.phx.gbl...
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

"Dmitry Karneyev" <ka******@msn.com> wrote in message
news:uf**************@TK2MSFTNGP10.phx.gbl...
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" <Mo************@hotmail.com> ???????/???????? ? ???????? ?????????: news:oprxjnygnahntkfz@localhost...
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.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/


Nov 15 '05 #6

P: n/a
Well, actually your variant is rather good!
I'll use it.

Thanks.

"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> сообщил/сообщила в новостях
следующее: news:ev**************@TK2MSFTNGP09.phx.gbl...
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

"Dmitry Karneyev" <ka******@msn.com> wrote in message
news:uf**************@TK2MSFTNGP10.phx.gbl...
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" <Mo************@hotmail.com> ???????/???????? ? ???????? ?????????: news:oprxjnygnahntkfz@localhost...
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.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/


Nov 15 '05 #7

P: n/a
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
}
}

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.