0

我有这段代码,我单击 Delete 键,它应该从 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);
            }
        }

但是,只要我按下删除键,我就会在 RemoveAt 行出现错误:

设置 DataSource 属性时无法修改项目集合。

现在我在 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;
        }

第二个地方:

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();
        }

那么我可以使用什么来代替 DataSource 或保留它?

编辑:

试过这个:

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;
                }
            }

        }

但是当单击 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);
            }

        }

在字符串 url 行:索引超出范围。必须是非负数且小于集合的大小

编辑:

我尝试使用标志:

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);
                }
            }
        }

和:

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;
                }
            }

        }

所以现在我没有收到错误,但它正在删除列表框中的所有项目我需要它来只删除现在/选定的一个即时消息。

4

2 回答 2

2

使用数据绑定时,您从数据源而不是 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;
                }
            }
        }
于 2013-02-24T14:40:55.373 回答
0

您可以直接添加到列表框中,如下所示:

   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();
于 2013-02-24T14:43:57.730 回答