Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从选中列表框中删除选定项目_C#_Winforms_Checkedlistbox - Fatal编程技术网

C# 从选中列表框中删除选定项目

C# 从选中列表框中删除选定项目,c#,winforms,checkedlistbox,C#,Winforms,Checkedlistbox,我编写了一个代码,将第一个checkedlistbox中的选定项传输到另一个checkedlistbox。这部分代码有效(但我仍然需要找到一种方法,使相同的选定项不会被反复写入)。我的第二个目标是从第一个checkedlistbox中删除选定项。我得到的错误是数组超出范围 private void button_ekle_Click(object sender, EventArgs e) { int i = 0; int k = 0; i =

我编写了一个代码,将第一个checkedlistbox中的选定项传输到另一个checkedlistbox。这部分代码有效(但我仍然需要找到一种方法,使相同的选定项不会被反复写入)。我的第二个目标是从第一个checkedlistbox中删除选定项。我得到的错误是数组超出范围

private void button_ekle_Click(object sender, EventArgs e)
{   
        int i = 0;
        int k = 0;
        i = clb_input.Items.Count; 
        //the number of items in the first checkedlistbox
        for (k = 0; k <i; k++)
        {
            if (clb_input.GetItemChecked(k) == true)
            {   
               //clb_output is the second checkedlistbox

                clb_output.Items.Add(clb_input.Items[k]);
                clb_input.Items.Remove(clb_input.CheckedItems[k]);
                i--;
            }
            else { }
        }
}
private void按钮\u ekle\u单击(对象发送者,事件参数e)
{   
int i=0;
int k=0;
i=clb_input.Items.Count;
//第一个checkedlistbox中的项目数

对于(k=0;k您的问题是由于在CheckedItems集合上使用索引器k造成的。
CheckedItems集合的元素数可能小于Items集合的计数,因此索引器可能具有不包含在CheckedItems集合中的值

但是,当您需要这种代码时,通常会反转循环。
从终点开始,走向起点

private void button_ekle_Click(object sender, EventArgs e)
{   
    for (int k = clb_input.Items.Count - 1; k >= 0; k--)
    {
        if (clb_input.GetItemChecked(k) == true)
        {   
            clb_output.Items.Add(clb_input.Items[k]);

            // Remove from the Items collection, not from the CheckedItems collection
            clb_input.Items.RemoveAt(k);
        }
        else { }
    }
}

您还应该记住,当您希望使用传统的for循环在集合上循环时,您的限制索引值是项目数减去1,因为网络中的每个集合都从索引0开始。因此,如果您有一个包含10个项目的集合,则有效索引从0到9。

您的问题是由于在Chec上使用了索引器k造成的凯迪特姆收藏。
CheckedItems集合的元素数可能小于Items集合的计数,因此索引器可能具有不包含在CheckedItems集合中的值

但是,当您需要这种代码时,通常会反转循环。
从终点开始,走向起点

private void button_ekle_Click(object sender, EventArgs e)
{   
    for (int k = clb_input.Items.Count - 1; k >= 0; k--)
    {
        if (clb_input.GetItemChecked(k) == true)
        {   
            clb_output.Items.Add(clb_input.Items[k]);

            // Remove from the Items collection, not from the CheckedItems collection
            clb_input.Items.RemoveAt(k);
        }
        else { }
    }
}

您还应该记住,当您希望使用传统的for循环在集合上循环时,您的限制索引值是项目数减去1,因为网络中的每个集合都从索引0开始。因此,如果您有一个包含10个项目的集合,则有效索引从0到9。

我已经按照您的建议重写了代码,我仍然是g设置相同的错误:\@melek\u 3将删除行更改为just
clb\u input.Items.RemoveAt(k);
@LarsTech我没有注意到。你完全正确。k不是CheckedItems集合中的有效索引。我将更新答案thanks@LarsTech是的,在最后提到,10个项目集合的0-9。我已经按照你的建议重写了代码,但仍然得到相同的错误:\@melek\u 3将删除行更改为just
clb_input.Items.RemoveAt(k);
@LarsTech我没有注意到。你完全正确。k不是CheckedItems集合中的有效索引。我将更新答案thanks@LarsTech是的,在最后提到,10件收藏品的价格是0-9。