简体   繁体   English

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

[英]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.

相关问题 System.ArgumentOutOfRangeException:索引超出范围 - System.ArgumentOutOfRangeException: Index was out of range System.ArgumentOutOfRangeException: '指定的参数超出了有效值的范围。 参数名称:asp.net中的index' - System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. Parameter name: index' in asp.net System.ArgumentOutOfRangeException:参数超出范围。 最短路径算法中的错误 - System.ArgumentOutOfRangeException: Argument is out of range. error in a shortest path algorithm listview-System.ArgumentOutOfRangeException - listview- System.ArgumentOutOfRangeException 抛出异常:System.ArgumentOutOfRangeException - Exception thrown: System.ArgumentOutOfRangeException C#System.ArgumentOutOfRangeException:索引超出范围 - C# System.ArgumentOutOfRangeException:Index was out of range System.ArgumentOutOfRangeException:索引超出范围 C# 中的错误 - System.ArgumentOutOfRangeException: Index was out of range Error in C# 我得到一个异常 System.ArgumentOutOfRangeException - I get an exception System.ArgumentOutOfRangeException 如何解决System.ArgumentOutOfRangeException异常? - How to solve System.ArgumentOutOfRangeException exception? 错误:System.ArgumentOutOfRangeException:索引超出范围。 必须为非负数且小于集合的大小 - Error: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM