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

Removing a ListView item using ContextMenu

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());
}
Nov 15 '05 #1
2 9543
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.

"VR" <Vi****************@gat.com> wrote in message news:1c****************************@phx.gbl...
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());
}

Nov 15 '05 #2
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, becausethe mouseDown event is triggered too often so you delete an item and after that the function is called againand 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.

"VR" <Vi****************@gat.com> wrote in message

news:1c****************************@phx.gbl...
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());
}

.

Nov 15 '05 #3

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

Similar topics

0
by: Bill Johnson | last post by:
I have a further question regarding an archive post at: ...
7
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I'v been struggeling with removing selected items from a listview. Anyone that can give me a piece of code that does this? I am a newbee to this C# and cant figure it out.... Regards...
1
by: RonNanko | last post by:
I am facing a userinteraction issue with a listview, which is populated/updated by a background task. While the population of the listview is taking place, the user should already be able to...
0
by: news.microsoft.com | last post by:
Hello, I have attached a contextmenustrip in the designer to a listview. When running, if there is no item in my listview, the contextmenu still shows. How can I hide it when no item is...
4
by: Pucca | last post by:
How can I tell a mouse right clicks over a listview item that's in a container panel. I only want to display a popup menu if the user right click the mouse over an item on the Listview. I don't...
5
by: Phill W. | last post by:
(VB'2003) What's the correct way to remove multiple, selected items from a ListView control (say, from a ContextMenu)? I ask because I'm getting a very annoying ArgumentOutOfRangeException...
5
by: Bart Steur | last post by:
Hi, What's the best way to programmaticly select (click) another listview Item after removing the selected/focused one. using VB2005 Express. Thanks Bart
12
by: Tom Bean | last post by:
I am trying to display a ContextMenuStrip when a user right-clicks on an item in a ListView and have encountered a something that seems strange to me. When the ListView is initially populated,...
1
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, In my application I need to periodically remove all current items from a ListView and add a new set into it. The following abbreviated code contains the basic idea: private void...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.