Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ComboBox remove from index problem

ComboBox remove from index problem

Scheduled Pinned Locked Moved C#
helpdatabasequestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rick van Woudenberg
    wrote on last edited by
    #1

    Gents, I'm experimenting with indexing and I came across the following problem that I cannot fully grasp. I have a combobox filled with stuff ( the collection items ). I want to remove it all at once so that the collection is empty again. So after some thinking I though I try the follwing : private void ClearComboBoxes() { for (int x = 0; x < cmbtrackWhere.Items.Count; x++) { cmbtrackWhere.Items.RemoveAt[x]; } } It gives me the error : " Cannot apply indexing [] to an expression of type 'method group' I assign the value zero to the integer x , count the items in the collection and add 1 to x if necessary until x is equal to the number of items in the collection. Then I remove each item on index [x] from the collection. Am I thinking in the right direction ? .. can anyone briefly explain to me what I'm doing wrong and why it fails ? kind regards,

    J L 2 Replies Last reply
    0
    • R Rick van Woudenberg

      Gents, I'm experimenting with indexing and I came across the following problem that I cannot fully grasp. I have a combobox filled with stuff ( the collection items ). I want to remove it all at once so that the collection is empty again. So after some thinking I though I try the follwing : private void ClearComboBoxes() { for (int x = 0; x < cmbtrackWhere.Items.Count; x++) { cmbtrackWhere.Items.RemoveAt[x]; } } It gives me the error : " Cannot apply indexing [] to an expression of type 'method group' I assign the value zero to the integer x , count the items in the collection and add 1 to x if necessary until x is equal to the number of items in the collection. Then I remove each item on index [x] from the collection. Am I thinking in the right direction ? .. can anyone briefly explain to me what I'm doing wrong and why it fails ? kind regards,

      J Offline
      J Offline
      J 0
      wrote on last edited by
      #2

      If you want to remove all of the items in a combobox, you should use: cmbtrackWhere.Items.Clear(); This will remove them all at once. If you are trying to remove an item at a specific index, i think your code should be something along the lines of: // Use parentheses instead of brackets cmbtrackWhere.Items.RemoveAt(x);

      1 Reply Last reply
      0
      • R Rick van Woudenberg

        Gents, I'm experimenting with indexing and I came across the following problem that I cannot fully grasp. I have a combobox filled with stuff ( the collection items ). I want to remove it all at once so that the collection is empty again. So after some thinking I though I try the follwing : private void ClearComboBoxes() { for (int x = 0; x < cmbtrackWhere.Items.Count; x++) { cmbtrackWhere.Items.RemoveAt[x]; } } It gives me the error : " Cannot apply indexing [] to an expression of type 'method group' I assign the value zero to the integer x , count the items in the collection and add 1 to x if necessary until x is equal to the number of items in the collection. Then I remove each item on index [x] from the collection. Am I thinking in the right direction ? .. can anyone briefly explain to me what I'm doing wrong and why it fails ? kind regards,

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        So the mistake was to use square brackects; RemoveAt is a method, it needs parentheses. Furthermore, as soon as you remove an item, all the higher-numbered items get moved down one positition, so if you need to remove al but the first N items the correct code would be:

        for (int x = N; x < cmbtrackWhere.Items.Count; x++) {
        cmbtrackWhere.Items.RemoveAt(N); // yes, always the first you dont want any more!
        }

        This loop continues to remove the first unwanted item, until there are no more unwanted items left. There is of course the alternative to remove them in reverse order:

        for (int x = cmbtrackWhere.Items.Count-1; x>=N; x--) {
        cmbtrackWhere.Items.RemoveAt(x);
        }

        Of course, if you want to remove everything there is the Clear() method to do that. :)

        Luc Pattyn [My Articles] [Forum Guidelines]

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups