Rob Garrett - Blogs

Welcome to Rob Garrett - Blogs Sign in | Join | Help
in Search
Google

Software/Technology Discussion

Software and Technology Tid-bits

Right-Click ListBox - Select Item

Useful to know - to select items in a ListBox with a right mouse click wire up the following code to the MouseDown event:
Share this post: Email it! | bookmark it! | digg it! | reddit!
Published Wednesday, April 05, 2006 3:20 PM by Rob Garrett

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Tony said:

Sweet--thanks for posting the example.
May 8, 2006 10:06 AM
 

Justin Dearing said:

Rob, I prefer this version it allows you to wire it to multiple listboxes. private void listBox_MouseDown(object sender, MouseEventArgs e) { ListBox lst = (ListBox) sender; // Select item if right click. if (MouseButtons.Right == e.Button) { Point pt = new Point(e.X, e.Y); lst.SelectedIndex = lst.IndexFromPoint(pt); } } Generally I prefer the "SenderType foo = (SenderType) sender;" pattern as opposed to calling the item by its class member name in events.
December 12, 2007 11:03 AM
 

Dewitt said:

Guess there are plenty of ways of doing this, easiest way i found was to send a left click before the right mouse button comes back up! Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal Y As Long) As Long Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long) Private Const MOUSEEVENTF_LEFTDOWN = &H2 Private Const MOUSEEVENTF_LEFTUP = &H4 Private Sub ListBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal Y As Single) If Button = 2 Then mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0& mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0& End If End Sub Like that! =]
February 19, 2008 5:12 AM

Leave a Comment

(required) 
(optional)
(required) 
Submit

Blurb


Head Shot
Rob Garrett is a British Expat living in Maryland USA. Rob is a trained software engineer and experienced in Windows .NET development.

Rob enjoys listening to Rock music, posting to blogs, driving in the country with the sunroof open, beer (not in conjunction with country driving) and spending time with his family.

This Blog

Syndication

Powered by Community Server, by Telligent Systems