Windows Develop Bookmark and Share   
 index > Windows Forms General > ListBox right click that selects item
 

ListBox right click that selects item

I need to capture the right click event for a list box and first select the item the mouse was over when clicked, then process the event.  How do I raise the MouseClick event with the button argument set to left from within an event handler for the MouseClick event?
MigrationUser 1  Tuesday, June 07, 2005 11:01 AM
fyi, this is what I came up with for a solution unless someone has an easier way:


private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e){
     if(e.Button == MouseButtons.Right)
     {
          for (int i = 0; i < listBox1.Items.Count; i++)
          {
               Rectangle r = new Rectangle(0, i*listBox1.ItemHeight, listBox1.Width, listBox1.ItemHeight);
               if (r.Contains(e.X, e.Y))
               {
                     listBox1.SelectedIndex = i;
                     i = listBox1.Items.Count;  //break the loop
               }
          }
     }
}

MigrationUser 1  Tuesday, June 07, 2005 12:32 PM

You can use google to search for other answers

Custom Search

More Threads

• how can I attach a excel file to an e-mail with Mail.MailMessage
• "Generic" Detection of Focus Change
• Determining events across multiple controls
• Handle use in vb.net
• How to print directly to printer
• Property gird: showing properties of class inside the selected class
• Converting 1.2E+7 (textbox) to a long causes exception
• Message Pumping
• how to ReadFile to enum type?
• Extend non-client area using DWM