gpt4 book ai didi

c# - 如何防止 SelectedIndexChanged 事件在从列表框中删除项目后在列表框中触发

转载 作者:太空宇宙 更新时间:2023-11-03 17:15:34 31 4
gpt4 key购买 nike

我正在努力选择一个将导致删除所选项目的项目。问题是,当我删除一个项目时,我选择了导致另一个删除的其他项目...

删除选定项目后如何取消选择/取消选择?

这导致了我的问题:

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

关于c# - 如何防止 SelectedIndexChanged 事件在从列表框中删除项目后在列表框中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8829393/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com