Removing a ListView item using ContextMenu

V

VR

I have a form with a list view control, a button control
and a main menu with menuDelete item. Any time I am
handling a button click event or menuDelete click event I
call DeleteItem() function which removes the selected item
from the list.

If I right-click on my list, I clone menuDelete item and
display a pop-up menu. So, if user selects Delete item in
the menu, I go through the same logic and remove the
selected item.

Here is the problem:
if I right-click on the list while the last item in the
list is selected, and then, choose Delete menu item I am
getting an exception in Main() --

"An unhandled exception of
type 'System.ArgumentOutOfRangeException' occurred in
system.windows.forms.dll

Additional information: Specified argument was out of the
range of valid values."

The exception is triggered AFTER I successfuly remove an
item from the list and leave DeleteItem() function. It
doesn't happen if I use a command button or a main menu to
remove the same item.

Also, it only happens when I remove the last item in the
list.

Also, if I set a break point in listView1_MouseDown()
function after .Show(...) is called and then take time
continuing -- the exception isn't thrown.

What am I doing wrong? Any help is greatly appreciated.

Thanks,
VR

Please see the code below.


private void button1_Click
(
object sender,
System.EventArgs e
)
{
DeleteItem();
}

protected void DeleteItem()
{
listView1.LabelEdit = true;
listView1.Items.RemoveAt(listView1.SelectedIndices[0]);
}

private void listView1_MouseDown
(
object sender,
System.Windows.Forms.MouseEventArgs e
)
{
if (e.Button == MouseButtons.Right)
{
ContextMenu oMenu = new ContextMenu();
oMenu.MenuItems.Add(menuDelete.CloneMenu());
oMenu.Show(this, new Point(e.X, e.Y));
}
}


private void menuDelete_Click
(
object sender,
System.EventArgs e
)
{
DeleteItem();
}


[STAThread]
static void Main()
{
Application.Run(new Form1());
}
 
T

timtos

I think I know your problem and had the same but unfortunately I can´t really remember the solution ;-)
But as you described below it is a time critical problem. I think to remember that this happens, because
the mouseDown event is triggered too often so you delete an item and after that the function is called again
and it then tries to delete another item but there is nothing more.

So it is perhaps better to use the MouseUp-Event!

And in your delete method: Wouldn´t it be better to test first if there really is a selected item.
Something like that:

if (listview.SelectedItems.Count > 0)
{
deleteItem();
}

I think otherwise you will always get an exception if your listview is empty!
Hope this helps at least a bit! Greetings,
timtos.
 
V

VR

Thanks, Timtos.

You are right -- it doesn't happen in MouseUp event.
Interestingly, I do have a code to check for the number of
selected items in the list (the code I posted I was
siplified) and it still didn't help.

Thanks, again.
VR
-----Original Message-----
I think I know your problem and had the same but
unfortunately I can´t really remember the solution ;-)
But as you described below it is a time critical problem.
I think to remember that this happens, because
the mouseDown event is triggered too often so you delete
an item and after that the function is called again
and it then tries to delete another item but there is nothing more.

So it is perhaps better to use the MouseUp-Event!

And in your delete method: Wouldn´t it be better to test
first if there really is a selected item.
Something like that:

if (listview.SelectedItems.Count > 0)
{
deleteItem();
}

I think otherwise you will always get an exception if your listview is empty!
Hope this helps at least a bit! Greetings,
timtos.

I have a form with a list view control, a button control
and a main menu with menuDelete item. Any time I am
handling a button click event or menuDelete click event I
call DeleteItem() function which removes the selected item
from the list.

If I right-click on my list, I clone menuDelete item and
display a pop-up menu. So, if user selects Delete item in
the menu, I go through the same logic and remove the
selected item.

Here is the problem:
if I right-click on the list while the last item in the
list is selected, and then, choose Delete menu item I am
getting an exception in Main() --

"An unhandled exception of
type 'System.ArgumentOutOfRangeException' occurred in
system.windows.forms.dll

Additional information: Specified argument was out of the
range of valid values."

The exception is triggered AFTER I successfuly remove an
item from the list and leave DeleteItem() function. It
doesn't happen if I use a command button or a main menu to
remove the same item.

Also, it only happens when I remove the last item in the
list.

Also, if I set a break point in listView1_MouseDown()
function after .Show(...) is called and then take time
continuing -- the exception isn't thrown.

What am I doing wrong? Any help is greatly appreciated.

Thanks,
VR

Please see the code below.


private void button1_Click
(
object sender,
System.EventArgs e
)
{
DeleteItem();
}

protected void DeleteItem()
{
listView1.LabelEdit = true;
listView1.Items.RemoveAt(listView1.SelectedIndices [0]);
}

private void listView1_MouseDown
(
object sender,
System.Windows.Forms.MouseEventArgs e
)
{
if (e.Button == MouseButtons.Right)
{
ContextMenu oMenu = new ContextMenu();
oMenu.MenuItems.Add(menuDelete.CloneMenu());
oMenu.Show(this, new Point(e.X, e.Y));
}
}


private void menuDelete_Click
(
object sender,
System.EventArgs e
)
{
DeleteItem();
}


[STAThread]
static void Main()
{
Application.Run(new Form1());
}


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top