简体   繁体   中英

Listbox remove wrong item

I have 3 ListBoxes in my page. I want to remove one selected item from ListBox. In ListBox1 it works good but when i select item from other ListBoxes and click remove - it remove always first item.

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

It's because the rest of your statements aren't being reached by your else if statements.

Try this:

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

If you want to Remove the ListBox Item you should always check all the ListBox's for selected Items , in your current code if first ListBox is not selected then it won't even go for checking the rest of the ListBox's as you have written if-else block .

Hence Change as Below:

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

Definitely not the best worded question.

Perhaps you only want to remove the currently selected item in whatever ListBox was last used?

If so, create a Form level variable to track what ListBox last changed its 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);
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM