Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ListView异常:System.ArgumentOutOfRangeException:参数超出范围_C#_.net_Winforms_Exception_Listview - Fatal编程技术网

C# ListView异常:System.ArgumentOutOfRangeException:参数超出范围

C# ListView异常:System.ArgumentOutOfRangeException:参数超出范围,c#,.net,winforms,exception,listview,C#,.net,Winforms,Exception,Listview,我正在尝试创建一个程序来运行在ListView中选择的特定应用程序。我的应用程序中有一个名为SoftView的列表视图,双击事件的代码如下: private void SoftView_DoubleClick(object sender, EventArgs e) { ... if (SoftView.Items[SoftView.SelectedIndices[0]].SubItems[0].Text == "Application name") { ..

我正在尝试创建一个程序来运行在ListView中选择的特定应用程序。我的应用程序中有一个名为SoftView的列表视图,双击事件的代码如下:

private void SoftView_DoubleClick(object sender, EventArgs e)

{

 ...
 if (SoftView.Items[SoftView.SelectedIndices[0]].SubItems[0].Text == "Application name")

 {

  ...
   -- Run selected application --
  Application.Exit();

 }

}
执行时,我有以下例外情况:

System.ArgumentOutOfRangeException:参数超出范围

参数名称:索引

在System.Windows.Forms.ListView+SelectedIndexCollection.get_项(Int32索引)[0x00000]

在Launcher.MainForm.SoftView_双击(System.Object sender,System.EventArgs e)[0x00000]

在System.Windows.Forms.Control.OnDoubleClick(System.EventArgs e)[0x00000]

在System.Windows.Forms.ListView+ItemControl.HandleClicks处(System.Windows.Forms.MouseEventArgs me)[0x00000]

位于System.Windows.Forms.ListView+ItemControl.ItemsMouseUp(System.Object sender,System.Windows.Forms.MouseevenTargets me)[0x00000]

位于System.Windows.Forms.Control.OnMouseUp(System.Windows.Forms.MouseEventArgs e)[0x00000]

位于System.Windows.Forms.Control.WmLButtonUp(System.Windows.Forms.Message&m)[0x00000]

位于System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message&m)[0x00000]

位于System.Windows.Forms.ListView+ItemControl.WndProc(System.Windows.Forms.Message&m)[0x00000]

位于System.Windows.Forms.Control+ControlWindowTarget.OnMessage(System.Windows.Forms.Message&m)[0x00000]

在System.Windows.Forms.Control+ControlNativeWindow.WndProc(System.Windows.Forms.Message&m)[0x00000]

位于System.Windows.Forms.NativeWindow.WndProc(IntPtr hWnd、Msg Msg、IntPtr wParam、IntPtr lParam)[0x00000]

有人知道如何解决这个问题吗?
提前感谢。

双击时没有选定的索引

SoftView.selectedDices
为空。因此
SoftView.SelectedIndices[0]
抛出异常

修复方法可能如下所示:

if (SoftView.Items.Count == 0)
    return;
if (SoftView.SelectedIndices.Count == 0)
    return;
if (SoftView.Items[SoftView.SelectedIndices[0]].SubItems[0].Text == "Application name")
    ...

这是实现ListView排序的最佳方法。如果您希望更改排序功能,请查看此处的示例。不,我不能这样做,很遗憾:(我有一个猜测,可能这个异常是因为在事件处理程序返回之前终止了应用程序。我想我应该在双击处理程序返回后终止应用程序并运行所选的应用程序。@Max-不看代码很难判断错误。从技术上讲,我回答了你的问题,并解释了为什么你有我想你需要展示更多的代码,可能还需要问另一个问题。