Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a listBox1 and a contextMenuStrip1 with a "delete" button.
I made it functional until now, but I must Left click an Item from listBox1 (to select it), then (anywhere) i click right and choose "delete"- it works but not as I really want.

How do I select an Item (from listBox1), using the RIGHT mouse button(to delete that selection only)?


//I use this event to get the mouse buttons:
private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (listBox1.Items.Count > 0)//if List is NOT empty
            {
                if (e.Button == MouseButtons.Right){
//..?..


Thanks.
Posted
Updated 4-Nov-11 11:05am
v2

You can find selected item then if it's selected delete it.Write these codes to
C#
if (e.Button == MouseButtons.Right){
Like :
C#
for (int i = 0; i < listBox1.Items.Count; i++)
{
    listBox1.SelectedIndex = i;
    Delete(listBox1.Items[i]); // Your delete Code.
}
 
Share this answer
 
Comments
_Q12_ 4-Nov-11 17:57pm    
Reformulate please!
How do I obtain a selection, by clicking with the RIGHT mouse btn inside that listbox?
After the clickEvent is triggered, Ill do the rest of the job (delete,add,etc).
Right now, i cant figure out, how to see the item selected.
I hope the question is more clear now.
_Q12_ 4-Nov-11 18:02pm    
It must have some connection with the "MousePosition" and the "listBox1.GetItemRectangle"
SercanOzdemir 4-Nov-11 18:26pm    
No you can get selected item with a prepared method.
Iam reformulating it for you Use This On Mouse Click Event of ListBox
:
C#
if (listBox1.Items.Count >; 0)//if List is NOT empty
            {if (e.Button == MouseButtons.Right){

for (int i = 0; i < listBox1.Items.Count; i++)
            {
                listBox1.SelectedIndex = i;
                //Your Delete CODE!
}
} // Your delete Code.            }
 
Share this answer
 
Comments
_Q12_ 4-Nov-11 18:28pm    
NO,Is not working!
I am afraid that you don't understand the question.
HERE:
This is all the code:
<pre>private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
if (listBox1.Items.Count > 0)//if List is NOT empty
{
if (e.Button == MouseButtons.Right)
{
deleteSelectedIndex = listBox1.SelectedIndex;
//Rectangle r = new Rectangle();
//r = listBox1.GetItemRectangle(0);

listBox1.SetSelected(deleteSelectedIndex, true);
contextMenuStrip1.Show(MousePosition); //select at mousePosision =?????????????????????????????
}
}
}</pre>
SercanOzdemir 4-Nov-11 18:37pm    
if you choose listbox1 Contextmenu property to your Contextmenu then it will show it on selection mouse position
_Q12_ 4-Nov-11 19:01pm    
No, it does not.
The contextmenu appear on mouse position, but the item from the list does NOT set selected. How can I select it?
_Q12_ 4-Nov-11 19:03pm    
Here is a test:
private void listBox1_MouseMove(object sender, MouseEventArgs e)
{
if (listBox1.GetItemRectangle(1).Contains(e.Location))
{
listBox1.SetSelected(1, true);//int index
}
}

BUT:
is selecting ONLY the [1] item from the list.
You see the problem now?
SercanOzdemir 4-Nov-11 19:31pm    
i already saw it but i think you handled it.If i helped you that was my pleasure.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900