如何防止SelectedIndexChanged事件在从ListBox中删除项后在其中激发
本文关键字:删除 在其中 ListBox SelectedIndexChanged 何防止 事件 | 更新日期: 2023-09-27 18:20:00
我正在选择一个项目,这将导致删除所选项目。问题是,当我删除一个项目时,我选择了导致另一个删除的附加项目。。。
删除所选项目后,如何取消选择/取消选择?
这导致了我的问题:
void lbMessage_SelectedIndexChanged(object sender, EventArgs e)
{
DialogResult result = new DialogResult();
result = MessageBox.Show("Are you sure you want to remove this item?", "Removal Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
lbMessage.Items.Remove(lbMessage.SelectedItem);
lbMessage.SelectedIndex = -1;
}
else
{
}
}
private void btnAddMessage_Click(object sender, EventArgs e)
{
lbMessage.Items.Add(txtMessage.Text);
txtMessage.Text = string.Empty;
}
在删除项目之前尝试删除SelectedIndexChanged
事件,然后将其添加回:
private void lbMessage_SelectedIndexChanged(object sender, EventArgs e) {
if (MessageBox.Show("Are you sure you want to remove this item?",
"Remove Confirmation",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes) {
lbMessage.SelectedIndexChanged -= lbMessage_SelectedIndexChanged;
lbMessage.Items.Remove(lbMessage.SelectedItem);
lbMessage.SelectedIndexChanged += lbMessage_SelectedIndexChanged;
}
}
设置SelectedIndex = -1
会引发SelectedIndexChanged
事件。检查事件开始时是否未选择任何内容。
void lbMessage_SelectedIndexChanged(object sender, EventArgs e)
{
if (lbMessage.SelectedIndex == -1) return;
...
}
如果你喜欢不带删除按钮的方式,那么就这样做:
bool isAfterDelete = false;
void lbMessage_SelectedIndexChanged(object sender, EventArgs e)
{
if (isAfterDelete)
{
isAfterDelete = false;
return;
}
DialogResult result = new DialogResult();
result = MessageBox.Show("Are you sure you want to remove this item?",
"Removal Confirmation",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
lbMessage.Items.Remove(lbMessage.SelectedItem);
isAfterDelete = true;
lbMessage.SelectedIndex = -1;
}
else
{
}
}
如果你只是在if()中包装你的代码,检查lbMessage.SelectedIndex==-1,或者在开头写一个检查,如果它是真的,返回:
void lbMessage_SelectedIndexChanged(object sender, EventArgs e)
{
if(lbMessage.SelectedIndex == -1)
return;
DialogResult result = new DialogResult();
result = MessageBox.Show("Are you sure you want to remove this item?", "Removal Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
lbMessage.Items.Remove(lbMessage.SelectedItem);
lbMessage.SelectedIndex = -1;
}
}
从Items集合中删除项目的剪切行为会导致项目更改其索引。因此,这会在列表框上再次调用"SelectedIndexChanged"事件。为了防止这种情况,您需要在remove函数之前设置一个标志,以指示在删除并防止事件发生。你可以通过以下方式来做到这一点。
private bool afterRemove = false;
void lbMessage_SelectedIndexChanged(object sender, EventArgs e)
{
if (afterRemove)
{
afterRemove = false;
return;
}
DialogResult result = new DialogResult();
result = MessageBox.Show("Are you sure you want to remove this item?", "Removal Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
afterRemove = true;
lbMessage.Items.Remove(lbMessage.SelectedItem);
// Another call to "lbMessage_SelectedIndexChanged" is made right here.
}
}
private void btnAddMessage_Click(object sender, EventArgs e)
{
lbMessage.Items.Add(txtMessage.Text);
txtMessage.Text = string.Empty;
}
一个可能更容易理解的替代方案可以是以下内容。
private bool afterRemove = false;
void lbMessage_SelectedIndexChanged(object sender, EventArgs e)
{
if (afterRemove)
return;
DialogResult result = new DialogResult();
result = MessageBox.Show("Are you sure you want to remove this item?", "Removal Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
afterRemove = true;
lbMessage.Items.Remove(lbMessage.SelectedItem);
// Another call to "lbMessage_SelectedIndexChanged" is made right here.
afterRemove = false;
}
}