473,395 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Select item from list box with right mouse click

Hi

My project has a form, two list boxes (lstArchive and lstSchedule) and two
buttons (btnSendArchive and btnSendSchedule)

Each list box represents the content of a directory. The command buttons
move the selected item out of one directory and into the other and refreshes
the list boxes to reflect the change in directory contents

I now want to add "context menu" functionality to the list boxes to preform
the same function as the two buttons. My problem is I can right click on the
list box and send the selected item to the other directory with the chosen
menu item ..... but right-clicking does not itself select the item over which
the mouse is hovering. In other words I have to left-click to select an item
before I can right-click to activate the contect menu for the selected item.

Is there any way that right-clicking in a list box can be used to select an
item in that box.

Any help would be appreciated

Regards

Michael Bond

Dec 6 '05 #1
5 17753

mabond wrote:
....
I now want to add "context menu" functionality to the list boxes to preform
the same function as the two buttons. My problem is I can right click on the
list box and send the selected item to the other directory with the chosen
menu item ..... but right-clicking does not itself select the item over which
the mouse is hovering. In other words I have to left-click to select an item
before I can right-click to activate the contect menu for the selected item.

Is there any way that right-clicking in a list box can be used to select an
item in that box.


Private Sub ListBox1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles ListBox1.MouseDown
If e.Button = MouseButtons.Right Then
Dim index As Integer = ListBox1.IndexFromPoint(New
Point(e.X, e.Y))
If index >= 0 Then
ListBox1.SelectedItem = ListBox1.Items(index)
End If
End If
End Sub

Because your context menu code fires on MouseUp (right?) this should
slot in just fine.

--
Larry Lard
Replies to group please

Dec 6 '05 #2
Hi, this should work

Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) _ Handles ListBox1.MouseDown
If e.Button = MouseButtons.Right Then
ListBox1.SelectedIndex = ListBox1.IndexFromPoint(e.X, e.Y)
End If
End Sub

Hth Greetz Peter

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"mabond" <ma****@discussions.microsoft.com> schreef in bericht
news:27**********************************@microsof t.com...
Hi

My project has a form, two list boxes (lstArchive and lstSchedule) and two
buttons (btnSendArchive and btnSendSchedule)

Each list box represents the content of a directory. The command buttons
move the selected item out of one directory and into the other and refreshes the list boxes to reflect the change in directory contents

I now want to add "context menu" functionality to the list boxes to preform the same function as the two buttons. My problem is I can right click on the list box and send the selected item to the other directory with the chosen
menu item ..... but right-clicking does not itself select the item over which the mouse is hovering. In other words I have to left-click to select an item before I can right-click to activate the contect menu for the selected item.
Is there any way that right-clicking in a list box can be used to select an item in that box.

Any help would be appreciated

Regards

Michael Bond

Dec 6 '05 #3
Peter, Larry

Thanks both .... especially for the speed of response.

Appreciate the help

Regards

Michael

"mabond" wrote:
Hi

My project has a form, two list boxes (lstArchive and lstSchedule) and two
buttons (btnSendArchive and btnSendSchedule)

Each list box represents the content of a directory. The command buttons
move the selected item out of one directory and into the other and refreshes
the list boxes to reflect the change in directory contents

I now want to add "context menu" functionality to the list boxes to preform
the same function as the two buttons. My problem is I can right click on the
list box and send the selected item to the other directory with the chosen
menu item ..... but right-clicking does not itself select the item over which
the mouse is hovering. In other words I have to left-click to select an item
before I can right-click to activate the contect menu for the selected item.

Is there any way that right-clicking in a list box can be used to select an
item in that box.

Any help would be appreciated

Regards

Michael Bond

Dec 6 '05 #4
"mabond" <ma****@discussions.microsoft.com> schrieb:
Is there any way that right-clicking in a list box can be used to select
an
item in that box.


\\\
Private Sub ListBox1_MouseUp( _
ByVal sender As Object, _
ByVal e As MouseEventArgs _
) Handles ListBox1.MouseUp
If e.Button = MouseButtons.Right Then
Dim n As Integer = Me.ListBox1.IndexFromPoint(e.X, e.Y)
If n <> ListBox.NoMatches Then
Me.ListBox1.SelectedIndex = n
End If
End If
End Sub
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Dec 6 '05 #5

Herfried K. Wagner [MVP] wrote:
Dim n As Integer = Me.ListBox1.IndexFromPoint(e.X, e.Y)
If n <> ListBox.NoMatches Then
Use of a meaningful named constant, now that's classy :)
Me.ListBox1.SelectedIndex = n


I can't believe I set SelectedItem to Items(n) :(

--
Larry Lard
Replies to group please

Dec 6 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Maurice Mertens | last post by:
Hi all, I want to cancel the right-mouse click on a control in VB.NET. Does anyone know how to do this? -- Met vriendelijke groet / With regards / Saludos, Moviat Automatisering
1
by: Sundar | last post by:
Hi I would like to center align the menu that gets displayed when right mouse is clicked. In MFC, the function TrackPopupMenu takes a parameter that determines the alignment of the menu (left,...
3
by: Gary | last post by:
Hi, How does one create the code to right mouse click in a rich text box area to say popup the edit menu that I have created in my text editor? When I imported my VB6 code into vb.net for the...
1
by: Nathan Williams via .NET 247 | last post by:
I'd like to be able to capture a Right Mouse Click event on aparticular Tab and popup a context menu to allow the user toclose the tab without the tab having to be the current selectedtab. Does...
6
by: jcrouse | last post by:
I have the following mouse events assigned to a label control. Is the a way I can tell which mouse button the users has clicked with? Private Sub lblP1JoyUp_Click(ByVal sender As System.Object,...
1
by: Pawan Singh | last post by:
Hi, Is it possible to select a list box item when someone right clicks on it? I can find the snippets of code for VB6 but that code will not work in VB.Net. Thanks Pawan
1
by: Joe Blow | last post by:
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...
0
by: johnmcpherson1 | last post by:
What's the best method for detecting a ctrl key being depressed simultaniously with a right mouse click in a datagrid in a vb application? I need to get row, field resolution. Regards, John
5
by: nuhfeken | last post by:
We have a C# winform that uses the MVP design pattern for the user interface. For reasons I'd rather not explain we need to simulate a right mouse click on a specific control to deactivate the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.