0

In my form have TextBox1 and ListBox1, buttonAdd, buttonRemove

buttonAdd => OK, I can do it. buttonRemove: When you delete a section: - Delete entry from textbox: Check one item in the listbox item should be deleted, if there are clear, if not, the message is not found - Delete the selected item in listbox

This is my idea:

     private void butonRemove_Click(object sender, EventArgs e)
    {
        if (textbox1.Text != "")
        {
            int i = 0;
            while (i <= listbox1.Items.Count)
            {
                string Item_remove = textbox1.Text;
                if (listbox1.Items[i].ToString().Contains(Item_remove))
                {
                    DialogResult conf_remove;
                    conf_remove = MessageBox.Show("Do you wwant to remove: " + listbox1.Items[i].ToString(), "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (conf_remove == DialogResult.Yes)
                    {
                        listbox1.Items.RemoveAt(i);
                        break;
                    }
                    else if (conf_remove == DialogResult.No)
                        i++;
                }
                else
                {
                    MessageBox.Show("Not found");
                    break;
                }
            }

            textbox1.Text = "";
            textbox1.Focus();
        }
        else if (listbox1.SelectedIndex < 0)
            MessageBox.Show("Please select item to remove");
        else
            listbox1.Items.Remove(listbox1.SelectedItem);

}

Please help me fix it, thank

user2485423
  • 1
  • 1
  • 3

3 Answers3

4

Here's the code for remove item.

private void buttonRemove_Click(object sender, EventArgs e) {
        if (listBox1.SelectedIndex == -1) { // Not Selected Anything
            MessageBox.Show("Select an item to delete");
        }
        else {
            listBox1.Items.RemoveAt(listBox1.SelectedIndex); // Remove item
        }
    }
0

Something like this?

void buttonRemove_Click(object sender, EventArgs e) 
{
    string matchcode = TextBox1.Text;

    ListItem item = this.ListBox1.Items.FindByText(matchcode);

    if (item != null)
    {
        //found
        this.ListBox1.Items.Remove(item);
    }
    else
    {
        //not found
        MessageBox.Show("is not found");
    }
}
0

This is what you are looking for, this controls all of the validation you need too.

if (ListBox.SelectedIndex == -1)
        {
            if (ListBox.NumberOfItems == 0) // if no index, need to select a row!
            {
                MessageBox.Show("No items to remove!");
            }
            else
            {
                MessageBox.Show("Please select an item first!");
            }
        }
        else
        {
             ListBox.Items.RemoveAt(lstStorage.SelectedIndex);
        }

This will remove the item that has been selected, with the validation that you require.

I have also commented some code to show you what parts mean. If you need any more explanations on how this works then just ask :)

Philip Gullick
  • 995
  • 7
  • 22
  • I want to know more about the case removed from the textbox entry, if there are several similar entries, eg THE THE man or woman, you need to check which ones you want to delete. If there is no entry in the listbox, the message is not found – user2485423 Jun 14 '13 at 14:05
  • Unfortunately I don't fully understand what you are asking, the validation in the if statements will validate if the listbox is empty or not. To validate text in the listbox then you would (to make life easier) validate the input by put the input to lower case. If you want to restrict what the user can input into the listbox then perhaps just use radio buttons with the terms they are allowed to use. However to make the textbox entry lowercase you just call the textbox.text.ToLower and that make validating phrases a lot easier. – Philip Gullick Jun 17 '13 at 07:31