Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
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:
C#
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 everyone
Posted
Comments
Abhinav S 14-Jun-13 9:27am    
Your idea is ok. Are you getting some errors?
[no name] 14-Jun-13 9:27am    
For some one to be able to tell you how to fix it, you would need to tell us what is broken.
Nguyễn Quang Nghĩa 14-Jun-13 9:37am    
when I remove the item, if the item in the listbox, but if not, then the error occurs
[no name] 14-Jun-13 10:20am    
What error? You have not said anything at all about any kind of an error yet.
sudipta biswas 14-Jun-13 9:37am    
Maybe you should just use listbox1.Items.Contains(textbox1.Text) instead of the loop.

Please try this
C#
if (listbox1.Items.Contains(Item_remove))
            {
            //ask for confirmation
            //if yes then
              listbox1.Items.Remove(Item_remove);
            }

        else
        {
            MessageBox.Show("Not found");
        }
 
Share this answer
 
Comments
Nguyễn Quang Nghĩa 14-Jun-13 10:07am    
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
XML
Here's the code for remove item.

<pre lang="cs">private void buttonRemove_Click(object sender, EventArgs e) {
        if (listBox1.SelectedIndex == -1) { // Not Selected Anything
            MessageBox.Show(&quot;Select an item to delete&quot;);
        }
        else {
            listBox1.Items.RemoveAt(listBox1.SelectedIndex); // Remove item
        }
    }</pre>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900