Having trouble finding the item of interest on a right mouse click

Hi,

I thought it would be kinds neat to display the detail when the user left clicks the item in a listbox, or provide a printout of the item if the user right clicks the item.  But when I intercept that a right click has occurred, I'm not able to find what the selection was.  The SelectedIndex is still the default of -1.

Does anybody know how to find the item which the user has right clicked on?  Is that part of the event which is passed into the event?

Thanks.
ba272Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
You can use ListBox.IndexFromPoint Method to count item index under current mouse position. Make Point from X, Y members of MouseEventHandler parameter and call IndexFromPoint.
Current selection is not changed by right click.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TheAvengerCommented:
This will show you the item that you clicked on with any button:

private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
      int index = this.listBox1.IndexFromPoint (e.X, e.Y);
      if (index >= 0)
            MessageBox.Show (this.listBox1.Items[index].ToString());
}

Attach the method to the MouseDown event of the text box
TheAvengerCommented:
Of the LIST box, sorry
TheAvengerCommented:
@AlexFM: sorry for repeating what you said, I just wrote the answer in the same time.
ba272Author Commented:
Thanks for the help.  The code helped too.

Bob
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.