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. Managed C++/CLI
  4. loop through comboboxes

loop through comboboxes

Scheduled Pinned Locked Moved Managed C++/CLI
tutorialquestion
7 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.
  • K Offline
    K Offline
    KlaasVersteeg
    wrote on last edited by
    #1

    Hi, Ik have a form containing a number of comboboxes, called cmb1, cmb2, etc. I want to loop through these boxes to set their itemlist. I have tried the following code:

    for each (Control^ box in this->Controls){
    if (box->GetType() == ComboBox::typeid){
    box->Items->Clear();
    }
    }

    This works fine, except that I cannot access the combobox's Items property. I can for instance access the Text property, so I think it's strange that the Items property is missing. Does anyone have an idea how to solve this? Thanks!

    Richard Andrew x64R 1 Reply Last reply
    0
    • K KlaasVersteeg

      Hi, Ik have a form containing a number of comboboxes, called cmb1, cmb2, etc. I want to loop through these boxes to set their itemlist. I have tried the following code:

      for each (Control^ box in this->Controls){
      if (box->GetType() == ComboBox::typeid){
      box->Items->Clear();
      }
      }

      This works fine, except that I cannot access the combobox's Items property. I can for instance access the Text property, so I think it's strange that the Items property is missing. Does anyone have an idea how to solve this? Thanks!

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      The reason you can't access the Items property is because you have declared box as a Control and Control doesn't have an Items collection. You would have to declare a new variable as ComboBox and cast box into it:

      for each (Control^ box in this->Controls){
      if (box->GetType() == ComboBox::typeid){
      ComboBox^ combo = (ComboBox^)box;
      combo->Items->Clear();
      }
      }

      modified on Friday, June 4, 2010 11:46 AM

      V K 3 Replies Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        The reason you can't access the Items property is because you have declared box as a Control and Control doesn't have an Items collection. You would have to declare a new variable as ComboBox and cast box into it:

        for each (Control^ box in this->Controls){
        if (box->GetType() == ComboBox::typeid){
        ComboBox^ combo = (ComboBox^)box;
        combo->Items->Clear();
        }
        }

        modified on Friday, June 4, 2010 11:46 AM

        V Offline
        V Offline
        voo doo12
        wrote on last edited by
        #3

        Have you tried this code

        for each(ComboBox^ box in this->Controls)
        {
        if(box->GetType() == ComboBox::typeid)
        {
        box->Items->Clear();
        }
        }

        K 1 Reply Last reply
        0
        • V voo doo12

          Have you tried this code

          for each(ComboBox^ box in this->Controls)
          {
          if(box->GetType() == ComboBox::typeid)
          {
          box->Items->Clear();
          }
          }

          K Offline
          K Offline
          KlaasVersteeg
          wrote on last edited by
          #4

          voo doo12, I've tried this, but when I run the program I get an error that says: "Unable to cast object of type 'System.Windows.Forms.Button' to type 'System.Windows.forms.ComboBox'. So it seems that if the program encounters a control which is not a ComboBox it cannot deal with it in this way.

          1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            The reason you can't access the Items property is because you have declared box as a Control and Control doesn't have an Items collection. You would have to declare a new variable as ComboBox and cast box into it:

            for each (Control^ box in this->Controls){
            if (box->GetType() == ComboBox::typeid){
            ComboBox^ combo = (ComboBox^)box;
            combo->Items->Clear();
            }
            }

            modified on Friday, June 4, 2010 11:46 AM

            K Offline
            K Offline
            KlaasVersteeg
            wrote on last edited by
            #5

            Richard, this makes sense. However, when I compile this code I get: error C2440: 'type cast' : cannot convert from 'System::Windows::Forms::Control ^' to 'System::Windows::Forms::ComboBox' Any idea how to solve this?

            1 Reply Last reply
            0
            • Richard Andrew x64R Richard Andrew x64

              The reason you can't access the Items property is because you have declared box as a Control and Control doesn't have an Items collection. You would have to declare a new variable as ComboBox and cast box into it:

              for each (Control^ box in this->Controls){
              if (box->GetType() == ComboBox::typeid){
              ComboBox^ combo = (ComboBox^)box;
              combo->Items->Clear();
              }
              }

              modified on Friday, June 4, 2010 11:46 AM

              K Offline
              K Offline
              KlaasVersteeg
              wrote on last edited by
              #6

              Solved! I made a typo in Richard's code (forgot the ^ symbol in the cast part). Now it works, thanks a lot for your help!

              Richard Andrew x64R 1 Reply Last reply
              0
              • K KlaasVersteeg

                Solved! I made a typo in Richard's code (forgot the ^ symbol in the cast part). Now it works, thanks a lot for your help!

                Richard Andrew x64R Offline
                Richard Andrew x64R Offline
                Richard Andrew x64
                wrote on last edited by
                #7

                Glad to be helpful. :)

                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