Add Remove Item sIn ComboBox !!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mironline
    New Member
    • May 2008
    • 24

    Add Remove Item sIn ComboBox !!!

    dear friends
    I want to add and remove items with this simple code :
    Code:
    private void comboBox1_KeyDown(object sender, KeyEventArgs e)
            {
    
                ComboBox c = sender as ComboBox;
                if (c == null) return;
                if (c.Text == "") return;
                if (e.KeyCode == Keys.Delete)
                {
                    c.Items.RemoveAt(c.FindStringExact(c.Text));
                }
                else if (e.KeyCode == Keys.Enter)
                    c.Items.Add(c.Text);
            }

    this exception appears after add and remove some data and Leave the control on Application.Run (new Form1()); method;

    InvalidArgument =Value of '0' is not valid for 'index'.


    thank you.
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    You'll need to check that the item exists in the combo-box before you try to delete it. At the moment you can type the name of a non-existent item, hit delete and the program will crash.

    Code:
    private void comboBox1_KeyDown(object sender, KeyEventArgs e)
    {
    
        ComboBox c = sender as ComboBox;
        if (c == null) return;
        if (c.Text == "") return;
        if (e.KeyCode == Keys.Delete)
        {
            int index = c.FindStringExact(c.Text);
            if (index > -1) c.Items.RemoveAt(c.FindStringExact(c.Text));
        }
        else if (e.KeyCode == Keys.Enter)
            c.Items.Add(c.Text);
    }

    Comment

    • Ramk
      New Member
      • Nov 2008
      • 61

      #3
      int index = c.FindStringExa ct(c.Text);
      if (index > -1) c.Items.RemoveA t(c.FindStringE xact(c.Text));
      I guess the above line should be
      Code:
      if (index > -1) c.Items.RemoveAt(index);

      Comment

      • nukefusion
        Recognized Expert New Member
        • Mar 2008
        • 221

        #4
        Originally posted by Ramk
        I guess the above line should be
        Code:
        if (index > -1) c.Items.RemoveAt(index);
        Thanks Ramk, that was what I had meant to write!

        Comment

        • mironline
          New Member
          • May 2008
          • 24

          #5
          Originally posted by nukefusion
          You'll need to check that the item exists in the combo-box before you try to delete it. At the moment you can type the name of a non-existent item, hit delete and the program will crash.

          if (index > -1) c.Items.RemoveA t(c.FindStringE xact(c.Text));
          Dear nukefusion
          I Guess the problem is so much stranger than we think .
          check this code :

          Code:
          private void comboBox1_KeyDown(object sender, KeyEventArgs e)
                  {
          
                      ComboBox c = sender as ComboBox;
                      if (c == null) return;
                      if (c.Text == "") return;
                      if (e.KeyCode == Keys.Delete)
                      {
          [B]                c.Items.Clear();[/B]
                      }
                      else if (e.KeyCode == Keys.Enter)
                          c.Items.Add(c.Text);
                  }
          same exception on Leave Event

          Comment

          • nukefusion
            Recognized Expert New Member
            • Mar 2008
            • 221

            #6
            I'm unable to recreate the exact exception you are having. I've created a simple form with a combo-box and a button and copied in the event handler code you provided. I can add, delete items, tab out of the combo-box, but cannot reproduce the error.

            Do you have any code executing in the Leave event? Exactly what steps are you taking to reproduce this exception?

            Comment

            • mironline
              New Member
              • May 2008
              • 24

              #7
              I just have one combobox in my page with one keydown event

              Code:
              private void comboBox1_KeyDown(object sender, KeyEventArgs e)
                      {
              
                          ComboBox c = sender as ComboBox;
                          if (c == null) return;
                          if (c.Text == "") return;
                          if (e.KeyCode == Keys.Delete)
                          {
                              c.Items.Clear();
                          }
                          else if (e.KeyCode == Keys.Enter)
                              c.Items.Add(c.Text);
                      }
              Add 3 items

              1 -> enter key
              2 -> enter key
              3 -> enter key


              PullDown The Combobox
              and Press Delete and Click on the Form to leave the combobox .

              The Exception appears now!

              Comment

              • nukefusion
                Recognized Expert New Member
                • Mar 2008
                • 221

                #8
                Thanks for the extra information. Using your steps I've been able to reproduce the error. It looks like if you delete an item while the drop-down is open and then try and leave the control, the selected index doesn't get a chance to update itself.

                You'll then get this error with the index parameter being equal to the index of
                whatever item you deleted.

                Really, the best way to add new items to a combo-box would be to use a separate control, i.e. a textbox, with a button to add that value to the list. The combo-box is only really intended for item selection. However, if you really want to do things this way, to get around this quirk you could handle the DropDownClosed event of the combo-box and do a quick range check yourself. Here's some example code:

                Code:
                private void comboBox1_DropDownClosed(object sender, EventArgs e)
                {
                    ComboBox c = sender as ComboBox;
                    if (c.SelectedIndex >= c.Items.Count) c.SelectedIndex = c.Items.Count - 1;
                }
                Using this method, when the drop-down closes if there is a mismatch between the selected index and the number of items in the list (an item has been deleted), then the last item in the list is selected.

                There are still other issues with this method that you'll probably want to clear up. For example, try typing some text, and pressing enter while the drop-down is open. The item will be added to the list twice.

                Comment

                • mironline
                  New Member
                  • May 2008
                  • 24

                  #9
                  thank you amigo

                  belive it or not you saved my life ;)

                  Comment

                  Working...