I'm trying to create a program to run a specific application, which was selected in a ListView. I have a ListView named SoftView in my application and the code for DoubleClick event is the following:
private void SoftView_DoubleClick(object sender, EventArgs e)
{
...
if (SoftView.Items[SoftView.SelectedIndices[0]].SubItems[0].Text == "Application name")
{
...
-- Run selected application --
Application.Exit();
}
}
On execution I have the following exception:
System.ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
at System.Windows.Forms.ListView+SelectedIndexCollection.get_Item (Int32 index) [0x00000]
at Launcher.MainForm.SoftView_DoubleClick (System.Object sender, System.EventArgs e) [0x00000]
at System.Windows.Forms.Control.OnDoubleClick (System.EventArgs e) [0x00000]
at System.Windows.Forms.ListView+ItemControl.HandleClicks (System.Windows.Forms.MouseEventArgs me) [0x00000]
at System.Windows.Forms.ListView+ItemControl.ItemsMouseUp (System.Object sender, System.Windows.Forms.MouseEventArgs me) [0x00000]
at System.Windows.Forms.Control.OnMouseUp (System.Windows.Forms.MouseEventArgs e) [0x00000]
at System.Windows.Forms.Control.WmLButtonUp (System.Windows.Forms.Message& m) [0x00000]
at System.Windows.Forms.Control.WndProc (System.Windows.Forms.Message& m) [0x00000]
at System.Windows.Forms.ListView+ItemControl.WndProc (System.Windows.Forms.Message& m) [0x00000]
at System.Windows.Forms.Control+ControlWindowTarget.OnMessage (System.Windows.Forms.Message& m) [0x00000]
at System.Windows.Forms.Control+ControlNativeWindow.WndProc (System.Windows.Forms.Message& m) [0x00000]
at System.Windows.Forms.NativeWindow.WndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam) [0x00000]
Does anyone know how to solve this problem? Thanks in advance.
There are no selected indexes at the moment when you double click
SoftView.SelectedIndices
is empty. So SoftView.SelectedIndices[0]
throws the exception.
The fix could be like this:
if (SoftView.Items.Count == 0)
return;
if (SoftView.SelectedIndices.Count == 0)
return;
if (SoftView.Items[SoftView.SelectedIndices[0]].SubItems[0].Text == "Application name")
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.