简体   繁体   English

invalidargument =值'8'对于'索引'无效

[英]invalidargument=value of '8' is not valid for 'index'

So im getting the error message invalidargument=value of '8' is not valid for 'index' but this should be fine since the listview has 9 (10) items. 因此,我收到错误消息invalidargument = value'8'对于'index'无效,但这应该没问题,因为listview有9(10)个项目。

在此处输入图片说明

在此处输入图片说明

My code is this: 我的代码是这样的:

 private async void backgroundWorker3_DoWork(object sender, DoWorkEventArgs e)
    {
        passArguments result = (passArguments)e.Argument;
        e.Result = result;

        while (running >= maxRunning)
        {
            editList("Waiting", result.passedFileName, result.passedNum, 1);
            await Task.Delay(500);

        }
        running++;
        editList("Loading...", result.passedFileName, result.passedNum, 0);
        //do stuff
    }

void editList(string message, string fileName, int number, int image)
{
    listView1.BeginUpdate();
    try
    {
        string[] row = { message, fileName };
        var listViewItem = new ListViewItem(row);
        listViewItem.ImageIndex = image;
        listView1.Items[number] = (listViewItem);
    }
    catch (Exception ex)
    {
        MessageBox.Show(number + " | " + ex.Message + Environment.NewLine + fileName + Environment.NewLine + message + Environment.NewLine + "COUNT: "+listView1.Items.Count);
    }

    listView1.EndUpdate();
}

But when I remove the while loop, it doesn't throw the error. 但是,当我删除while循环时,它不会引发错误。 Im not sure why this is, can anyone help me out? 我不确定为什么会这样,有人可以帮助我吗?

Edit: 编辑:

Stack Trace 堆栈跟踪

System.ArgumentException was unhandled by user code
  HResult=-2147024809
  Message=InvalidArgument=Value of '8' is not valid for 'index'.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.ListView.ListViewNativeItemCollection.RemoveAt(Int32 index)
       at System.Windows.Forms.ListView.ListViewNativeItemCollection.set_Item(Int32 displayIndex, ListViewItem value)
       at System.Windows.Forms.ListView.ListViewItemCollection.set_Item(Int32 index, ListViewItem value)
       at ***.Form1.editList(String message, String fileName, Int32 number, Int32 image) in ***\Form1.cs:line 347
       at ***.Form1.<backgroundWorker3_DoWork>d__c.MoveNext() in ***\Form1.cs:line 369
  InnerException: 

From the looks of things, you're trying to assign an item to an element in listview1.Items in a position that is out of range. 从外观上,您试图将一个项目分配给listview1.Items中超出范围的位置的元素。

listView1.Items[number] will not expand the number of items if number is not in range of the listview. 如果number不在listview范围内,则listView1.Items[number]将不会扩展项目数。

I would propose something like this: 我会提出这样的建议:

void editList(string message, string fileName, int number, int image)
{
    ListViewItem dummyrow = new ListViewItem(new string[] {"loading", ""});
    listView1.BeginUpdate();
    try
    {
        string[] row = { message, fileName };
        var listViewItem = new ListViewItem(row);
        listViewItem.ImageIndex = image;
        while (listView1.Items.length <= number) {
          //if the listview doesn't have enough rows yet,
          //add a loading message as placeholder
          listView1.Items.add(dummyrow);
        }
        listView1.Items[number] = (listViewItem);
    }
    catch (Exception ex)
    {
        MessageBox.Show(number + " | " + ex.Message + Environment.NewLine + fileName + Environment.NewLine + message + Environment.NewLine + "COUNT: "+listView1.Items.Count);
    }

    listView1.EndUpdate();
}

The following does not provide an answer but is more like a collection of hints I have found searching for an answer (I will delete this post when there is a real answer) 以下内容不提供答案,但更像是我在寻找答案时发现的一些提示(如果有真实答案,我将删除此帖子)

Again the stack trace: 再次是堆栈跟踪:

  System.ArgumentException was unhandled by user code   HResult=-2147024809
  Message=InvalidArgument=Value of '8' is not valid for 'index'.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.ListView.ListViewNativeItemCollection.RemoveAt(Int32 index)
       at System.Windows.Forms.ListView.ListViewNativeItemCollection.set_Item(Int32 displayIndex, ListViewItem value)
       at System.Windows.Forms.ListView.ListViewItemCollection.set_Item(Int32 index, ListViewItem value)
       at ***.Form1.editList(String message, String fileName, Int32 number, Int32 image) in ***\Form1.cs:line 347
       at ***.Form1.<backgroundWorker3_DoWork>d__c.MoveNext() in ***\Form1.cs:line 369

The following code is taken from .Net-Framework 4.5.2; 以下代码摘自.Net-Framework 4.5.2; I added own comments into the code (but not with // to make clear which ones are mine) 我在代码中添加了自己的注释(但不添加//来表明属于我的注释)

The System.ArgumentException is thrown here: System.ArgumentException抛出在这里:

public virtual void RemoveAt(int index) {
                if (index < 0 || index >= owner.columnHeaders.Length)
                    throw new ArgumentOutOfRangeException("index", SR.GetString(SR.InvalidArgument, "index", (index).ToString(CultureInfo.CurrentCulture)));

                int w = owner.columnHeaders[index].Width; // Update width before detaching from ListView

                // in Tile view our ListView uses the column header collection to update the Tile Information
                if (owner.IsHandleCreated && this.owner.View != View.Tile) {

          !!! important: 
                int retval = unchecked( (int) (long)owner.SendMessage(NativeMethods.LVM_DELETECOLUMN, index, 0));
                                          ^-----^----> quite strange...

        _________________________________________________
        |  Here it is (probably):    
        |            if (0 == retval)
        |                throw new ArgumentException(SR.GetString(SR.InvalidArgument,
        |                                                          "index",
        |                                                          (index).ToString(CultureInfo.CurrentCulture)));
                }

the value of retval is defined by: retval的值定义为:

internal IntPtr SendMessage(int msg, int wparam, int lparam) {
            Debug.Assert(IsHandleCreated, "Performance alert!  Calling Control::SendMessage and forcing handle creation.  Re-work control so handle creation is not required to set properties.  If there is no work around, wrap the call in an IsHandleCreated check.");
            return UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), msg, wparam, lparam);
        }

which calls the method: 调用方法:

[DllImport(ExternDll.User32, CharSet = CharSet.Auto)]
[ResourceExposure(ResourceScope.None)]
public static extern IntPtr SendMessage(HandleRef hWnd, int msg, int wParam, int lParam);

Here are some comments to this method from PInvoke.net 这是来自PInvoke.net对此方法的一些评论

1) Use IntPtr instead of UIntrPtr: The UIntPtr type is not CLS-compliant 1)使用IntPtr而不是UIntrPtr:UIntPtr类型不符合CLS

2) NEVER use "int" or "integer" as lParam. 2)切勿将“ int”或“ integer”用作lParam。 Your code WILL crash on 64-bit windows. 您的代码将在64位Windows上崩溃。 ONLY use IntPtr, a "ref" structure, or an "out" structure. 仅使用IntPtr,“ ref”结构或“ out”结构。

3) NEVER use "bool", "int", or "integer" as the return value. 3)永远不要使用“ bool”,“ int”或“ integer”作为返回值。 Your core WILL crash on 64-bit windows. 您的核心将在64位Windows上崩溃。 ONLY use IntPtr. 仅使用IntPtr。 It's not safe to use bool - pInvoke cannot marshal an IntPtr to a boolean. 使用bool并不安全-pInvoke无法将IntPtr编组为布尔值。

[...] [...]

2) The return IntPtr of SendMessage might be IntPtr.Zero 2)SendMessage的返回IntPtr可能是IntPtr.Zero


As you can see lparam is 0 (-> integer) when SendMessage is called. 如您所见,调用SendMessage时, lparam0 (->整数)。 (I personally don't think that this is the reason for our problem; but it could.) (我个人不认为这是导致我们出现问题的原因;但是可以。)


I hope I have helped those who want to go deeper into this problem. 我希望我对那些想深入研究这个问题的人有所帮助。

I guess it could be useful to know why IntPtr could be IntPtr.Zero 我想知道为什么 IntPtr可以是IntPtr.Zero可能IntPtr.Zero

Happy researching! 研究愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 InvalidArgument =值&#39;4&#39;对于&#39;索引&#39;无效 - InvalidArgument=Value of '4' is not valid for 'index' InvalidArgument =值&#39;1&#39;对&#39;索引&#39;无效 - InvalidArgument=Value of '1' is not valid for 'index' InvalidArgument =值&#39;3&#39;对于&#39;索引&#39;无效。 参数名称:索引 - InvalidArgument=Value of'3' is not valid for 'index'. parameter name: index InvalidArgument=&#39;-1&#39; 的值对 &#39;index&#39; 无效。 参数名称:索引 - InvalidArgument=Value of '-1' is not valid for 'index'. Parameter name: index 附加信息:InvalidArgument =值“ 0”对“索引”无效 - Additional information: InvalidArgument=Value of '0' is not valid for 'index' C# InvalidArgument = '2' 的值对于 'index' 无效 - C# InvalidArgument = Value of '2' is not valid for 'index' ListView中的错误:InvalidArgument =值&#39;0&#39;对&#39;index&#39;无效 - Error in ListView: InvalidArgument = Value of '0' is not valid for 'index' Listview.count-InvalidArgument =值“ 0”对“索引”无效 - Listview.count - InvalidArgument=Value of '0' is not valid for 'index c#InvalidArgument =值&#39;-1&#39;对&#39;索引&#39;无效 - c# InvalidArgument=Value of '-1' is not valid for 'index' InvalidArgument =值&#39;6&#39;对于索引无效。 参数名称:索引 - InvalidArgument=Value of '6' is not valid for index. Parameter name: index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM