Updating/changing Items in a ListBox with a TextBox
-
Hello, I currently have the problem that I want to update the contents (a string) of an item of a listbox with a textbox. Best would be on-the-fly so I tried
listBox1.Items[listBox1.SelectedIndex] = textBox1.Text
in TextChanged. However I also want to be able to select which item to update by selecting the item in that very listbox. So I do something liketextBox1.Text = listBox1.Items[listBox1.SelectedIndex]
in SelectedIndexChanged of the listbox. (I say "something like" because the information includes other numbers that are also stored in a list along with the text.) The problem with that is that selecting an item changes the text in the textbox and that again updates the item which implicitly calls an SelectedIndexChanged again (or so it seems). And during that updating of the listbox it happens that the selectedindex changes to -1. Which is a problem if you try to read that item... I was wondering if there is an easier/better/cleaner way to accomplish what I try? :confused: Stephan -
Hello, I currently have the problem that I want to update the contents (a string) of an item of a listbox with a textbox. Best would be on-the-fly so I tried
listBox1.Items[listBox1.SelectedIndex] = textBox1.Text
in TextChanged. However I also want to be able to select which item to update by selecting the item in that very listbox. So I do something liketextBox1.Text = listBox1.Items[listBox1.SelectedIndex]
in SelectedIndexChanged of the listbox. (I say "something like" because the information includes other numbers that are also stored in a list along with the text.) The problem with that is that selecting an item changes the text in the textbox and that again updates the item which implicitly calls an SelectedIndexChanged again (or so it seems). And during that updating of the listbox it happens that the selectedindex changes to -1. Which is a problem if you try to read that item... I was wondering if there is an easier/better/cleaner way to accomplish what I try? :confused: StephanSimple fix, just add
if (listBox1.SelectedIndex > -1)
so that the conditional is evaluated before making any updates. This should resolve your issue and update the reruired data. For example:private void textBox1_TextChanged(object sender, EventArgs e) { if (listBox1.SelectedIndex > -1) { listBox1.Items[listBox1.SelectedIndex] = textBox1.Text; } } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { if (listBox1.SelectedIndex > -1) { textBox1.Text = listBox1.Items[listBox1.SelectedIndex].ToString(); } }
-Arcond -
Simple fix, just add
if (listBox1.SelectedIndex > -1)
so that the conditional is evaluated before making any updates. This should resolve your issue and update the reruired data. For example:private void textBox1_TextChanged(object sender, EventArgs e) { if (listBox1.SelectedIndex > -1) { listBox1.Items[listBox1.SelectedIndex] = textBox1.Text; } } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { if (listBox1.SelectedIndex > -1) { textBox1.Text = listBox1.Items[listBox1.SelectedIndex].ToString(); } }
-ArcondThanks for your answer, I found another solution myself today.:) Stephan