我有listbox1
绑定到名为DataTable
的{{1}},我在DataTable的单元格中设置值的行更改Notes
然后再设置listBox1.SelectedIndex = -1
1}}到原始值并完成。因此该行调用listBox1.SelectedIndex
事件两次。
相反,它甚至不应该调用该事件。如何解决这个问题?
listBox1_SelectedIndexChanged
答案 0 :(得分:3)
暂时删除SelectedIndexChanged处理程序
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
listBox1.SelectedIndexChanged -= listBox1_SelectedIndexChanged;
if (listBox1.SelectedIndex != -1)
{
dataSet11.Notes.Rows[listBox1.SelectedIndex]["Text"] = richTextBox1.Text;
//this line fires listBox1_SelectedIndexChanged event
//with listBox1.SelectedIndex = -1
//then it changes listBox1.SelectedIndex back to original value before complete execution, and calls the even again
}
listBox1.SelectedIndexChanged += listBox1_SelectedIndexChanged;
}
答案 1 :(得分:2)
您可以使用触发器布尔字段:
private TextBox_OntextChanged(object sender, EventArgs args)
{
this.supressEvents = true;
//Do your stuff here
this.supressEvents = false;
}
private void ListBox_OnSelectionChanged(object sender, EventArgs args)
{
if (this.supressEvents)
{
return;
}
//Do your stuff here
}