[英]ListView exception: System.ArgumentOutOfRangeException: Argument is out of range
I'm trying to create a program to run a specific application, which was selected in a ListView. 我正在尝试创建一个程序来运行在ListView中选择的特定应用程序。 I have a ListView named SoftView in my application and the code for DoubleClick event is the following:
我的应用程序中有一个名为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();
}
}
On execution I have the following exception: 在执行时,我有以下异常:
System.ArgumentOutOfRangeException: Argument is out of range.
System.ArgumentOutOfRangeException:参数超出范围。
Parameter name: index
参数名称:索引
at System.Windows.Forms.ListView+SelectedIndexCollection.get_Item (Int32 index) [0x00000]
在System.Windows.Forms.ListView + SelectedIndexCollection.get_Item(Int32索引)处[0x00000]
at Launcher.MainForm.SoftView_DoubleClick (System.Object sender, System.EventArgs e) [0x00000]
在Launcher.MainForm.SoftView_DoubleClick(System.Object发件人,System.EventArgs e)处[0x00000]
at System.Windows.Forms.Control.OnDoubleClick (System.EventArgs e) [0x00000]
在System.Windows.Forms.Control.OnDoubleClick(System.EventArgs e)处[0x00000]
at System.Windows.Forms.ListView+ItemControl.HandleClicks (System.Windows.Forms.MouseEventArgs me) [0x00000]
在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]
在System.Windows.Forms.ListView + ItemControl.ItemsMouseUp(System.Object发送者,System.Windows.Forms.MouseEventArgs,我)处[0x00000]
at System.Windows.Forms.Control.OnMouseUp (System.Windows.Forms.MouseEventArgs e) [0x00000]
在System.Windows.Forms.Control.OnMouseUp(System.Windows.Forms.MouseEventArgs e)处[0x00000]
at System.Windows.Forms.Control.WmLButtonUp (System.Windows.Forms.Message& m) [0x00000]
在System.Windows.Forms.Control.WmLButtonUp(System.Windows.Forms.Message&m)处[0x00000]
at System.Windows.Forms.Control.WndProc (System.Windows.Forms.Message& m) [0x00000]
在System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message&m)[0x00000]
at System.Windows.Forms.ListView+ItemControl.WndProc (System.Windows.Forms.Message& m) [0x00000]
在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]
在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]
在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]
在System.Windows.Forms.NativeWindow.WndProc(IntPtr hWnd,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. SoftView.SelectedIndices
为空。 So SoftView.SelectedIndices[0]
throws the exception. 因此,
SoftView.SelectedIndices[0]
引发异常。
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")
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.