Click to See Complete Forum and Search --> : [RESOLVED] raising an event


Feoggou
December 21st, 2009, 07:53 AM
Hello.

I have a listbox. And I want to select the proper item when the user right-clicks an item. An ideea I had was to use MouseDown event because the item is selected when the left mouse button is down, but I DON'T KNOW how to do that.

Can you please help me?

memeloo
December 21st, 2009, 08:21 AM
the listbox raises the mousedown event and you only have to attach an event handler. you don't know how to do it? you should read about events in c#.

nelo
December 21st, 2009, 09:08 AM
Hello.

I have a listbox. And I want to select the proper item when the user right-clicks an item. An ideea I had was to use MouseDown event because the item is selected when the left mouse button is down, but I DON'T KNOW how to do that.

Can you please help me?

I would also add that the item in the listbox is selected when click it with the left mouse button. That is the behaviour of the list box. You don't need to do anything to get that to work. The event is raised for you as explained by mememoo and you can add a handler for it easily. So the question becomes why do you need right mouse button click? Normally that is used for context menus that pop up...Is this the case with you?

Feoggou
December 21st, 2009, 09:41 AM
So the question becomes why do you need right mouse button click?

well, I need to use a context menu for the menuitem that is underneath the cursor (that is, the cursor is above that menuitem). it is odd to always left-click and right click to have the needed item selected (the context menu is for the menuitem, not for the listbox as a whole).

memeloo
December 21st, 2009, 09:55 AM
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
this.listBox1.SelectedIndex = this.listBox1.IndexFromPoint(e.Location);
}

Feoggou
December 21st, 2009, 10:00 AM
thanks a lot.