简体   繁体   English

当尝试从listBox中删除/删除项目时为什么我得到一个错误:项目集合无法修改?

[英]When trying to remove/delete an item from the listBox why im getting an error: Items collection cannot be modified?

I have this code where I click the Delete key it should delete an item from the listBox: 我有这个代码,我点击删除键,它应该从列表框中删除一个项目:

private void listBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (this.listBox1.SelectedIndex >= 0)
                    this.listBox1.Items.RemoveAt(this.listBox1.SelectedIndex);
            }
        }

But as soon as im pressing the delete key im getting an error on the RemoveAt line: 但是只要我按下删除键我就会在RemoveAt行上出错:

Items collection cannot be modified when the DataSource property is set. 设置DataSource属性时,无法修改项集合。

Now I have in other two places in my Form1 where im using the DataSource: First: 现在我在我的Form1中的其他两个地方使用DataSource:首先:

private void ListBoxLoadKeys(Dictionary<string, List<string>> dictionary, string FileName)
        {
            using (StreamReader sr = new StreamReader(FileName))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    int i = line.Count();
                    tokens = line.Split(',');
                    dictionary.Add(tokens[0], tokens.Skip(1).ToList());
                    data.Add("Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens[1]);
                }
            }
            listBox1.DataSource = data;
        }

Second place: 第二个地方:

private void ClearListBox()
        {
            data.Clear();
            listBox1.DataSource = null;
            string sb;
            foreach (KeyValuePair<string, List<string>> kvp in LocalyKeyWords)
            {
                for (int i = 0; i < kvp.Value.Count(); i++)
                {
                    sb = "Url: " + kvp.Key + " --- " + "Local KeyWord: " + kvp.Value[i] + Environment.NewLine;
                    data.Add(sb.ToString());
                }
            }
            listBox1.DataSource = data;
            listBox1.Select();
        }

So what can I use instead the DataSource or to keep it ? 那么我可以使用什么代替DataSource或保留它呢?

Edited: 编辑:

Tried this: 试过这个:

private void listBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (this.listBox1.SelectedIndex >= 0)
                {
                    string obj = this.listBox1.SelectedValue.ToString();
                    data.Remove(obj);
                    listBox1.DataSource = null;
                    listBox1.DataSource = data;
                }
            }

        }

But when clicking the Delete key im getting an error here: 但是当点击Delete键时,我在这里收到错误:

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            string url = data[e.Index].Substring(0, 5);

            using (Font f = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular))
            {
                ColorText.ColorListBox(data, e);
            }

        }

On the string url line: Index was out of range. 在字符串url行:索引超出范围。 Must be non-negative and less than the size of the collection 必须是非负数且小于集合的大小

Edited: 编辑:

I tried to use a flag: 我试图使用一个标志:

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (deletedKey != true)
            {
                string url = data[e.Index].Substring(0, 5);

                using (Font f = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular))
                {
                    ColorText.ColorListBox(data, e);
                }
            }
        }

And: 和:

private void listBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (this.listBox1.SelectedIndex >= 0)
                {
                    deletedKey = true;
                    string obj = this.listBox1.SelectedValue.ToString();
                    data.Remove(obj);
                    listBox1.DataSource = null;
                    listBox1.DataSource = data;
                }
            }

        }

So now im not getting errors but it's deleting all the items in the listBox I need it to delete only the one im on now/selected. 所以现在我没有得到错误,但它正在删除listBox中的所有项目我需要它只删除现在/选择的一个。

When using data binding, you remove from the data source and not the ListBox. 使用数据绑定时,从数据源而不是ListBox中删除。

private void listBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (this.listBox1.SelectedIndex >= 0)
                {
                   string obj = this.listBox1.SelectedValue.ToString();
                   data.Remove(obj);
                   listBox1.DataSource = null;
                   listBox1.DataSource = data;
                }
            }
        }

You can add directly to the listbox as follows: 您可以直接添加到列表框,如下所示:

   listbox1.BeginUpdate();
   for (int i = 0; i < kvp.Value.Count(); i++)
                {
                    sb = "Url: " + kvp.Key + " --- " + "Local KeyWord: " + kvp.Value[i] + Environment.NewLine;
                    listbox1.Add(sb.ToString());
                }
   listbox1.EndUpdate();

暂无
暂无

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

相关问题 列表框错误:设置了DataSource属性时,无法修改项目集合 - listbox error: Items collection cannot be modified when the DataSource property is set 如何在不同的 colors 中为列表框中的项目着色? 出现异常:设置 DataSource 属性后无法修改 Items 集合 - How to color items in listBox in different colors? getting exception : Items collection cannot be modified when the DataSource property is set 我在尝试为listBox中的文本着色时收到ArgumentOutOfRange异常,为什么? - Im getting ArgumentOutOfRange exception when trying to color text in a listBox why? C#ListBox:设置DataSource属性时,无法修改Items集合 - C# ListBox : Items collection cannot be modified when the DataSource property is set 无法从列表框删除项目 - Cannot remove items from ListBox 从列表框C#中删除项目时将剪切项目 - Items are cut when remove item from listbox c# 当我试图进入相对路径时,我得到的错误开始索引不能小于零 - When im trying to getting into a relative path im getting an error startindex cannot be less than zero 当我只是尝试从 gridview 获取单元格的值时,我收到有关日期时间的错误 - Im getting an error about Datetime when Im just trying get the value of a cell from a gridview 如果项目存在于另一个ListBox中,则从ListBox中删除项目 - Remove items from ListBox if item exists in another ListBox 集合被修改; 从 LIstBox 中删除 ListItem 时,枚举可能不会执行错误 - Collection was modified; enumeration may not execute error when removing a ListItem from a LIstBox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM