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());
}
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());
}