Selecting index in ComboBox with right mouse click

J

Joe Blow

I would like to set the index in a ComboBox with the item under the
mouse when I right click the mouse. I have set up a context menu with
2 items, all that works great. I want to execute the instructions on
the item under the mouse but I can't get it to select with the right
mouse. Anyone know how I can select an item with the right mouse
button?

Help would be greatly appreciated,

Joe
 
J

John Opincar

This should do the trick:

private void listBox1_MouseDown(object sender, MouseEventArgs e) {
Point p = new Point(e.X, e.Y);
if ( (e.Button & MouseButtons.Right) == MouseButtons.Right ) {
for ( int idx = 0; idx < listBox1.Items.Count; idx++ ) {
if ( listBox1.GetItemRectangle(idx).Contains(p) ) {
listBox1.SelectedIndex = idx;
break;
}
}
}
}

John Opincar
(e-mail address removed)
 

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