简体   繁体   中英

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.

在此处输入图片说明

在此处输入图片说明

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. 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[number] will not expand the number of items if number is not in range of the listview.

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; I added own comments into the code (but not with // to make clear which ones are mine)

The System.ArgumentException is thrown here:

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:

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

1) Use IntPtr instead of UIntrPtr: The UIntPtr type is not CLS-compliant

2) NEVER use "int" or "integer" as lParam. Your code WILL crash on 64-bit windows. ONLY use IntPtr, a "ref" structure, or an "out" structure.

3) NEVER use "bool", "int", or "integer" as the return value. Your core WILL crash on 64-bit windows. ONLY use IntPtr. It's not safe to use bool - pInvoke cannot marshal an IntPtr to a boolean.

[...]

2) The return IntPtr of SendMessage might be IntPtr.Zero


As you can see lparam is 0 (-> integer) when SendMessage is called. (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

Happy researching!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM