By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,635 Members | 2,060 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,635 IT Pros & Developers. It's quick & easy.

right click to select listbox item...

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.