简体   繁体   中英

Deleting a double clicked item from a listbox using message box ok button?

So far I have managed to get this far on my double click event and have the message box showing accurately except I get a overload error when trying to add exclamation icon, But my main issue is how to code to get the selected list box item to delete when clicking ok button from message box?

private void statesListBox_MouseDoubleClick(object sender, MouseEventArgs e)
{
    //Select a state to remove from list box
    if (statesListBox.SelectedItem != null)

    if (statesListBox.SelectedItem.ToString().Length != 0)

    MessageBox.Show("Delete" + " " + (statesListBox.SelectedItem.ToString()) 
                    + "   " + "Are you sure?", "Delete" + " " + 
                    (statesListBox.SelectedItem.ToString()));
    if (MessageBoxResult.OK)
}

You need to capture the SelectedIndices property so you can remove items. If you use this property directly, the .RemoveAt calls will cause the selection to change, and you can't work with it. As well, you should iterate collections in reverse index order when removing multiple items, or the loop will remove wrong items after the first. This should do the trick;

int[] indices = (from int i in statesListBox.SelectedIndices orderby i descending select i).ToArray();

foreach (int i in indices)
    statesListBox.Items.RemoveAt(i);
private void statesListBox_MouseDoubleClick(object sender, MouseEventArgs e)
{
    //Select a state to remove from list box
    if (statesListBox.SelectedItem != null)
        return;

    if (statesListBox.SelectedItem.ToString().Length != 0)
    {            
        if (
            MessageBox.Show("Are you sure you want to delete " + 
                            statesListBox.SelectedItem.ToString() + "?", "Delete" 
                            + statesListBox.SelectedItem.ToString(), 
                            MessageBoxButtons.YesNo, MessageBoxIcon.Information) 
            == DialogResult.Yes
       )
        statesListBox.Items.Remove(statesListBox.SelectedItem);
    }
}

First things first.

Above code will delete your selected item when Yes is pressed. Since you are asking a question that's why answers can in the form of yes and no.

Secondly according to your comment ( Any idea what is causing the overload error when I try adding the exclamation icon to my message box? I believe it is a error caused by the toString ) given to RJLohan answer , there is an overload error. After some thinking i guess i have got why and what error you are taking about

You must be calling MessageBox.Show as

MessageBox.Show Method (String, String, MessageBoxIcon)

while the right syntax is

MessageBox.Show Method (String, String, MessageBoxButtons, MessageBoxIcon)

That's why error must be saying "The best overload method match for 'System.Windows.Forms.MessageBox.Show(string, string, System.Windows.MessageBoxButtons)' has some invalid arguments."

or something like that.

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