简体   繁体   English

C# InvalidArgument = '2' 的值对于 'index' 无效

[英]C# InvalidArgument = Value of '2' is not valid for 'index'

I am new to C# and I have encountered an error stating that: InvalidArgument=Value of '2' is not valid for 'index'.我是 C# 的新手,我遇到了一个错误,指出:InvalidArgument='2' 的值对 'index' 无效。

I want to set the items in checkedlistbox checked if there is a match in listbox.如果列表框中有匹配项,我想设置检查列表框中的项目。 Can anyone help me with this problem.谁能帮我解决这个问题。

This the part of my code where the problems appear.这是我的代码中出现问题的部分。

for (int i = 0; i < checklistbox.Items.Count; i++)
{
    if (checklistbox.Items[i].ToString() == listbox.Items[i].ToString())
     {
        //Check only if they match! 
        checklistbox.SetItemChecked(i, true);
     }
}

The reason you are getting this error is because you are looping through the count of checklistbox items.您收到此错误的原因是因为您正在循环检查清单框项目的计数。 So, for example if there are 3 items in that array, and listbox only has 2 items, then on the third loop (when i = 2), you are trying to reference an item in the listbox array that does not exist.因此,例如,如果该数组中有 3 个项目,而列表框只有 2 个项目,那么在第三个循环中(当 i = 2 时),您将尝试引用列表框数组中不存在的项目。

Another way to do it would be like this:另一种方法是这样的:

foreach (var item in listbox.Items)
        {
            if (Array.Exists(checklistbox.Items, lbitem => lbitem.ToString() == item.ToString()))
            {
               //They match! 
               checklistbox[item].MarkAsChecked()
            }
        }

Update: answer updated to add MarkAsChecked() and loop through user inputted values held within checklist array.更新:更新答案以添加 MarkAsChecked() 并循环遍历清单数组中保存的用户输入值。

You just need to use nested for loop.您只需要使用嵌套的 for 循环。 Here is the code.这是代码。

 for (int i = 0; i < listbox.Items.Count; i++)
 {
   for (int j = 0; j < checkedlistbox.Items.Count; j++)
   {
     if (listbox.Items[i].ToString() == checkedlistbox.Items[j].ToString())
     {
       //Check only if they match! 
       checkedlistbox.SetItemChecked(i, true);
     }
   }
 }

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

相关问题 c#InvalidArgument =值&#39;-1&#39;对&#39;索引&#39;无效 - c# InvalidArgument=Value of '-1' is not valid for 'index' invalidargument =值&#39;8&#39;对于&#39;索引&#39;无效 - invalidargument=value of '8' is not valid for 'index' 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' c #databound ComboBox:InvalidArgument =值'1'对'SelectedIndex'无效 - c# databound ComboBox : InvalidArgument=Value of '1' is not valid for 'SelectedIndex' 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' 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM