【问题标题】:Listbox remove wrong item列表框删除错误的项目
【发布时间】:2013-11-06 16:31:48
【问题描述】:

我的页面中有 3 个列表框。我想从 ListBox 中删除一个选定的项目。在 ListBox1 中效果很好,但是当我从其他 ListBoxes 中选择项目并单击删除时 - 它总是删除第一个项目。

 protected void Button4_Click(object sender, EventArgs e)
    {
        if (ListBox1.SelectedIndex != -1)
        {
            ListBox1.Items.Remove(ListBox1.SelectedItem);
        }
        else if (ListBox2.SelectedIndex != -1)
        {
            ListBox2.Items.Remove(ListBox2.SelectedItem);
        }
        else if (ListBox3.SelectedIndex != -1)
        {
            ListBox3.Items.Remove(ListBox3.SelectedItem);
        }
    }

【问题讨论】:

  • 既不清楚期望的行为是什么,也不清楚您当前看到的行为是什么;请澄清这些观点。

标签: c# visual-studio-2012


【解决方案1】:

这是因为您的 else if 语句没有到达您的其余语句。

试试这个:

protected void Button4_Click(object sender, EventArgs e)
{
        if (ListBox1.SelectedIndex != -1)
            ListBox1.Items.Remove(ListBox1.SelectedItem);

        if (ListBox2.SelectedIndex != -1)
            ListBox2.Items.Remove(ListBox2.SelectedItem);

        if (ListBox3.SelectedIndex != -1)
            ListBox3.Items.Remove(ListBox3.SelectedItem);
}

【讨论】:

  • 要求似乎是只应删除一项,而不是各一项。
  • @Servy,这意味着if 语句仍然是问题,但它们需要return
  • @neoistheone 或者只使用else if 而不是三个if 语句;)
  • @Servy,大声笑,是的,我想就是这样。伙计,我想我需要再来一杯咖啡;或者休息一下!
  • @Servy 很好的观察。哈哈
【解决方案2】:

绝对不是措辞最好的问题。

也许您只想删除上一次使用的 ListBox 中的当前选定项?

如果是这样,创建一个表单级变量来跟踪 ListBox 上次更改其 SelectedIndex 的位置:

public partial class Form1 : Form
{

    private ListBox CurrentListBox = null;

    public Form1()
    {
        InitializeComponent();
        ListBox1.SelectedIndexChanged += new EventHandler(ListBox_SelectedIndexChanged);
        ListBox2.SelectedIndexChanged += new EventHandler(ListBox_SelectedIndexChanged);
        ListBox3.SelectedIndexChanged += new EventHandler(ListBox_SelectedIndexChanged);
    }

    void ListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        CurrentListBox = (ListBox)sender;
    }

    private void button4_Click(object sender, EventArgs e)
    {
        if (CurrentListBox != null && CurrentListBox.SelectedIndex != -1)
        {
            CurrentListBox.Items.Remove(CurrentListBox.SelectedItem);
        }
    }

}

【讨论】:

  • 那很好,但是,最好(因为使用当前项目)在新方法中重构(button4 处理程序的)代码并在 selectedindex 处理程序中调用它,删除项目.
  • @terrybozzio 但是,一旦您选择它,该项目就会消失......可能不是用户期望的。如果他们点击了错误的项目怎么办?!
  • 哦,如果在 if 语句中发生这种情况,您可以放置​​一个消息框,例如警告用户如果结果正常,将删除哪些项目删除它,否则放置 CurrentListBox.SelectedIndex = -1 ;...用户可以愉快地选择另一个:)。
  • @terrybozzio 我希望我永远不必使用您的任何应用程序。
  • 什么,你想让这个例子做一个整批验证?常见的......啊,或者仅仅是因为我对你的回答说了些什么,你才以一种不太成熟的方式回复我。顺便说一句,我说你的回答很好,并且仍然在这里为我认为 OP 想要的做最好的 + 1,如果我冒犯了你的感受,对不起
【解决方案3】:

如果您想删除 ListBox 项,您应该始终检查所有 ListBox 中的选定项,在您当前的代码中,如果未选择第一个 ListBox,那么它甚至不会像您所写的那样检查其余的 ListBox if-else 块。

因此更改如下:

protected void Button4_Click(object sender, EventArgs e)
        {
            if (ListBox1.SelectedIndex != -1)
            {
                ListBox1.Items.Remove(ListBox1.SelectedItem);
            }
            if (ListBox2.SelectedIndex != -1)
            {
                ListBox2.Items.Remove(ListBox2.SelectedItem);
            }
            if (ListBox3.SelectedIndex != -1)
            {
                ListBox3.Items.Remove(ListBox3.SelectedItem);
            }
        }

【讨论】:

  • +1 因为你也是正确的。但是你必须更好地解释自己。这种变化非常微妙,不容易看到。
  • @neoistheone 我认为这种变化是显而易见的。但这只是我……
  • 1) 这只是一个低质量的答案;它根本没有描述变化。正确但不清楚是很成问题的 2)根本不清楚这是理想的行为。 OP 声明点击应该删除 one 项,而不是从 each 列表框中删除一项。
  • @Sudhakar 什么?这种评论毫无意义。
  • 如果没有帮助就使用 if 而不是 else
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-07
  • 2010-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多