2011-06-16 193 views
0

我试图创建一个程序来运行特定的应用程序,这是在ListView中选择的。我有一个在我的应用程序和DoubleClick事件的代号为SoftView的ListView如下:ListView异常:System.ArgumentOutOfRangeException:参数超出范围

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发送)[0x00000]

在System.Windows.Forms.ListView + ItemControl.HandleClicks(System.Windows.Forms的.MouseEventArgs我)[0x0000 0]

在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 &米)[0x00000]

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

在System.Windows.Forms.ListView + ItemControl.WndProc(System.Windows.Forms.Message &米)[0x00000]

在System.Windows.Forms.Control的+ ControlWindowTarget.OnMessage(System.Windows。 Forms.Message &米)[0x00000]

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

在System.Windows.Forms的。 NativeWindow.WndProc(IntPtr hWnd,Msg msg,IntPtr wParam,IntPtr lParam)[0x00000]

有谁知道如何解决这个问题? 在此先感谢。

+0

这是实现分选一个ListView的最佳途径。如果你想改变你的排序功能,可以看看这里的例子。 http://support.microsoft。com/kb/319401 – CharithJ 2011-06-16 04:55:35

回答

1

有此时没有选择的指标,当你双击

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") 
    ... 
+0

我有这样的检查,但它仍然不工作:( – Max 2011-06-16 04:34:10

+0

而我点击现有的项目当然 – Max 2011-06-16 04:38:54

+0

不,我不能这样做不幸:( – Max 2011-06-16 04:58:52