[英]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.