简体   繁体   中英

Changing from single select to multiple select in ListBox winform C#

if (productListBox.SelectedIndex >= 0)
                {
                    bool canDelete = true;
                    for (int i = 0; i < bkmNameListBox.Items.Count; i++)
                    {
                        if (((string[])bkmList[i])[1] == _IgnoredBKMID)
                        {
                            canDelete = false;
                        }
                    }
                    if (canDelete)
                    {
                        if (MessageBox.Show("Deleting a product will DELETE ALL debug BKM of this particular product,\r\n Continue?", "Delete Product", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            DatabaseAccess.DeleteProduct(productListBox.SelectedItem.ToString());
                            LoadListBox1();
                            bkmNameListBox.Items.Clear();
                        }
                    }
                    else
                    {
                        MessageBox.Show("You cannot delete this product because one if its Debug BKM is in editing", "Access Denied");
                    }
                }

Above code to delete ONE item in productListBox. how can i change to delete Multiple item ? thanks.

try this:

    private void Form1_Load(object sender, EventArgs e)
    {
        bkmNameListBox.SelectionMode = SelectionMode.MultiExtended;
    }
    private void button1_Click(object sender, EventArgs e)
    {

        while (bkmNameListBox.SelectedItems.Count > 0)
        {
            Removedatabasefiled(bkmNameListBox.SelectedItems[0]);
            bkmNameListBox.Items.Remove(bkmNameListBox.SelectedItems[0]);
        }
    }
listBox1.SelectionMode = SelectionMode.MultiExtended;

Loop through the SelectedIndices and Remove them one by one as the comment link's says

for (int i = listBox1.SelectedIndices.Count-1; i >= 0; i--)
    {
     listBox1.Items.RemoveAt(listBox1.SelectedIndices[i]);

      //Delete from datasource, in case indices are the same  
      DatabaseAccess.DeleteProduct(listBox1.SelectedIndices[i]);
    }

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