![](/img/trans.png)
[英]System.ArgumentOutOfRangeException: Index was out of range
[英]ListView exception: System.ArgumentOutOfRangeException: Argument is out of range
我正在尝试创建一个程序来运行在ListView中选择的特定应用程序。 我的应用程序中有一个名为SoftView的ListView,DoubleClick事件的代码如下:
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_Item(Int32索引)处[0x00000]
在Launcher.MainForm.SoftView_DoubleClick(System.Object发件人,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发送者,System.Windows.Forms.MouseEventArgs,我)处[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消息,IntPtr wParam,IntPtr lParam)处[0x00000]
有谁知道如何解决这个问题? 提前致谢。
双击当前没有选定的索引
SoftView.SelectedIndices
为空。 因此, 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")
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.