473,396 Members | 1,966 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,396 software developers and data experts.

right click to select listbox item...

If you look at explorer, right clicking on a file, first selects the file,
then throws up the context menu relating to that selection.

With a Windows ListBox control and a simple context menu, the default
behaviour seems to display the context menu on the listbox, but there is no
selection.

Now I know that explorer's using a listview, and that the context menu acts
differently if you click on open space (no selection), but can I simulate a
right click selection in a listbox?
Is my approach wrong in what I'm trying to achieve?
Is what I'm trying to acheive even possible?

Thanks.

Daniel.
Nov 16 '05 #1
6 29964
Dan,

It should be relatively simple. Basically, you want to detect when the
mouse button is pushed down (not released). Create an event handler for the
mouse down event on the listbox. Then, you can pass the coordinates of the
mouse down button (assuming it is the right click button) and pass it to the
IndexFromPoint method on the listbox, which will return the index of the
item at that point. From there, you can select that item.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:uz**************@TK2MSFTNGP09.phx.gbl...
If you look at explorer, right clicking on a file, first selects the file,
then throws up the context menu relating to that selection.

With a Windows ListBox control and a simple context menu, the default
behaviour seems to display the context menu on the listbox, but there is
no selection.

Now I know that explorer's using a listview, and that the context menu
acts differently if you click on open space (no selection), but can I
simulate a right click selection in a listbox?
Is my approach wrong in what I'm trying to achieve?
Is what I'm trying to acheive even possible?

Thanks.

Daniel.

Nov 16 '05 #2
Nicholas,

Thanks, I'll try that out! =o)

Dan.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:OZ**************@TK2MSFTNGP12.phx.gbl...
Dan,

It should be relatively simple. Basically, you want to detect when the
mouse button is pushed down (not released). Create an event handler for
the mouse down event on the listbox. Then, you can pass the coordinates
of the mouse down button (assuming it is the right click button) and pass
it to the IndexFromPoint method on the listbox, which will return the
index of the item at that point. From there, you can select that item.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:uz**************@TK2MSFTNGP09.phx.gbl...
If you look at explorer, right clicking on a file, first selects the
file, then throws up the context menu relating to that selection.

With a Windows ListBox control and a simple context menu, the default
behaviour seems to display the context menu on the listbox, but there is
no selection.

Now I know that explorer's using a listview, and that the context menu
acts differently if you click on open space (no selection), but can I
simulate a right click selection in a listbox?
Is my approach wrong in what I'm trying to achieve?
Is what I'm trying to acheive even possible?

Thanks.

Daniel.


Nov 16 '05 #3
It sort of works... When the click occurs, the popup occurs, the selection
is made. But because the context menu is active, the list box doesn't look
to have changed focus. Then when you leave the context menu, the selection
is seen.

Any ideas?

my code for the mouse up is as follows.

private void listBox1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
int indexover = listBox1.IndexFromPoint ( e.X, e.Y );
if ( indexover >= 0 && indexover < listBox1.Items.Count )
{
listBox1.SelectedIndex = indexover;
}
listBox1.Refresh();
}
Nov 16 '05 #4
Dan,

You did it in the mouse up event. You should do it in the mouse down
event, as this mirrors the functionality in explorer. If you right-click in
explorer, and hold the button down, you will notice that the selection is
made, but the context menu does not pop up. Only when you release the
button does the context menu show up.

Also, do you need to make a call to Refresh? I don't think you do.

Finally, make sure you check to see that the right mouse button is
clicked.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:Oi**************@TK2MSFTNGP14.phx.gbl...
It sort of works... When the click occurs, the popup occurs, the selection
is made. But because the context menu is active, the list box doesn't look
to have changed focus. Then when you leave the context menu, the selection
is seen.

Any ideas?

my code for the mouse up is as follows.

private void listBox1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
int indexover = listBox1.IndexFromPoint ( e.X, e.Y );
if ( indexover >= 0 && indexover < listBox1.Items.Count )
{
listBox1.SelectedIndex = indexover;
}
listBox1.Refresh();
}

Nov 16 '05 #5
Yep that does the trick, thanks again for your help.

Happy New Year!
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:es**************@TK2MSFTNGP09.phx.gbl...
Dan,

You did it in the mouse up event. You should do it in the mouse down
event, as this mirrors the functionality in explorer. If you right-click
in explorer, and hold the button down, you will notice that the selection
is made, but the context menu does not pop up. Only when you release the
button does the context menu show up.

Also, do you need to make a call to Refresh? I don't think you do.

Finally, make sure you check to see that the right mouse button is
clicked.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:Oi**************@TK2MSFTNGP14.phx.gbl...
It sort of works... When the click occurs, the popup occurs, the
selection is made. But because the context menu is active, the list box
doesn't look to have changed focus. Then when you leave the context menu,
the selection is seen.

Any ideas?

my code for the mouse up is as follows.

private void listBox1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
int indexover = listBox1.IndexFromPoint ( e.X, e.Y );
if ( indexover >= 0 && indexover < listBox1.Items.Count )
{
listBox1.SelectedIndex = indexover;
}
listBox1.Refresh();
}


Nov 16 '05 #6
"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> schrieb:
If you look at explorer, right clicking on a file, first selects the file,
then throws up the context menu relating to that selection.

With a Windows ListBox control and a simple context menu, the default
behaviour seems to display the context menu on the listbox, but there is
no selection.


\\\
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

' Show context menu here using 'ContextMenu.Show'...
End If
End If
End Sub
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 16 '05 #7

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

Similar topics

3
by: RockNRoll | last post by:
Greetings, Can I select an item in a listbox based on user textbox entry and a submit button? Can you provide any code examples for what needs to occur when the user clicks submit? Thank you,...
1
by: Juan Romero | last post by:
Hey guys, I have a small problem here.... I have a treeview control. I want to display a popup menu when the user right clicks on a node. This is no problem, and I am able to make the popup come...
1
by: BillyB | last post by:
I have a context menu tied to a list box. When I right-click on an item, I'd like that item to be selected before the context menu invokes, similar to how Outlook Express works when right-clicking...
2
by: Niels Jensen | last post by:
Is there a way to select an item ina listbox with the right click event? basically I want a user to be able to rightclick an item which will select the item and then bring up the context menu which...
1
by: skOOb33 | last post by:
I have a context menu for a datagrid that shows up fine. I am wanting to set it to where when a user right clicks anywhere on the grid, it will select the row the mouse is over (if it's over one),...
3
by: Art | last post by:
Hi, I've got a bunch of ListBoxes. I want to have a context menu that will include, for example, an edit routine. The problem I have is the following. Suppose you select item 1. Then you move...
1
by: akameswaran | last post by:
I have a list box, I'd like to generate a right click menu for items in the list box. The problem is unless I left click the item first, I can't figure out which item in the list I clicked over. ...
1
by: johnnypung | last post by:
I have a treeview opening a contextMenuStrip on right click. However the right click would not select the node that it right clicks on. How should I make it so the right click will select the node,...
0
by: tarun | last post by:
Hello All, Please find the code for a simple wx.TreeCtrl code. Whenever I right click on any item in the Tree, I see the function, 'OnSelChanged' gets called twice. What I've just done is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.