Pit998
Pit998

Reputation: 49

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

Upvotes: 0

Views: 140

Answers (3)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

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

Upvotes: 1

Idle_Mind
Idle_Mind

Reputation: 39122

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

}

Upvotes: 1

gleng
gleng

Reputation: 6304

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

Upvotes: 3

Related Questions