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. More Efficient Code

More Efficient Code

Scheduled Pinned Locked Moved C#
c++tutorialquestion
8 Posts 4 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.
  • D Offline
    D Offline
    Debs
    wrote on last edited by
    #1

    The current program I am working on has a lot of user Checkboxs and currently when ever I change a property I end up coding something like this: CheckBox1->Enabled=true; CheckBox2->Enabled=true; ... CheckBox30->Enabled=true; this is obviously not a very efficient way of doing this... so I'm looking for a more efficient way... maybe something along the lines of for(int c=0;c!=31;c++) { CheckBox(c)->Enabled=true; } this code will obviously not work but any suggestions on how to make something like this work??? :~ Cheers* Debs

    T R D 3 Replies Last reply
    0
    • D Debs

      The current program I am working on has a lot of user Checkboxs and currently when ever I change a property I end up coding something like this: CheckBox1->Enabled=true; CheckBox2->Enabled=true; ... CheckBox30->Enabled=true; this is obviously not a very efficient way of doing this... so I'm looking for a more efficient way... maybe something along the lines of for(int c=0;c!=31;c++) { CheckBox(c)->Enabled=true; } this code will obviously not work but any suggestions on how to make something like this work??? :~ Cheers* Debs

      T Offline
      T Offline
      therealmccoy
      wrote on last edited by
      #2

      If all of your checkboxes are located in a container control (panel, groupbox) or if they are the only controls on your form then you could use a foreach statement. foreach(Control aControl in form1.controls) { CheckBox aCheckBox = (CheckBox) aControl; aCheckBox.Enabled = true; } I did not test the above code. It's just off the top of my head. You could also create and arraylist of checkboxes and interate through then the same way. www.lovethosetrains.com -- modified at 13:10 Thursday 22nd September, 2005

      1 Reply Last reply
      0
      • D Debs

        The current program I am working on has a lot of user Checkboxs and currently when ever I change a property I end up coding something like this: CheckBox1->Enabled=true; CheckBox2->Enabled=true; ... CheckBox30->Enabled=true; this is obviously not a very efficient way of doing this... so I'm looking for a more efficient way... maybe something along the lines of for(int c=0;c!=31;c++) { CheckBox(c)->Enabled=true; } this code will obviously not work but any suggestions on how to make something like this work??? :~ Cheers* Debs

        R Offline
        R Offline
        Robert Rohde
        wrote on last edited by
        #3

        You could put all CheckBoxes into an array and iterate over it:

        //instance member
        CheckBox[] _allCheckBoxes;

        //somewhere once all CheckBoxes are created
        _allCheckBoxes = new CheckBox[31];
        _allCheckBoxes[0] = CheckBox1;
        _allCheckBoxes[1] = CheckBox2;
        //...

        //then if you have to change all of them
        foreach (CheckBox cb in _allCheckBoxes)
        cb.Enabled = true;

        N 1 Reply Last reply
        0
        • R Robert Rohde

          You could put all CheckBoxes into an array and iterate over it:

          //instance member
          CheckBox[] _allCheckBoxes;

          //somewhere once all CheckBoxes are created
          _allCheckBoxes = new CheckBox[31];
          _allCheckBoxes[0] = CheckBox1;
          _allCheckBoxes[1] = CheckBox2;
          //...

          //then if you have to change all of them
          foreach (CheckBox cb in _allCheckBoxes)
          cb.Enabled = true;

          N Offline
          N Offline
          Niklas Ulvinge
          wrote on last edited by
          #4

          Are you sure thats possible? When I try to do it it wont work, it give's me A LOT of errors and all kinds of warnings. Niklas Ulvinge aka IDK

          R 1 Reply Last reply
          0
          • N Niklas Ulvinge

            Are you sure thats possible? When I try to do it it wont work, it give's me A LOT of errors and all kinds of warnings. Niklas Ulvinge aka IDK

            R Offline
            R Offline
            Robert Rohde
            wrote on last edited by
            #5

            Well... yes Im sure :). If you want I can mail you a small sample.

            D N 2 Replies Last reply
            0
            • R Robert Rohde

              Well... yes Im sure :). If you want I can mail you a small sample.

              D Offline
              D Offline
              Debs
              wrote on last edited by
              #6

              Thanks for the suggestions I will try them out:-D Debs

              1 Reply Last reply
              0
              • R Robert Rohde

                Well... yes Im sure :). If you want I can mail you a small sample.

                N Offline
                N Offline
                Niklas Ulvinge
                wrote on last edited by
                #7

                Is it possible to do it with menuitems? And sure, mail the sample to me... My email is: ulvinge@gmail.com Niklas Ulvinge aka IDK

                1 Reply Last reply
                0
                • D Debs

                  The current program I am working on has a lot of user Checkboxs and currently when ever I change a property I end up coding something like this: CheckBox1->Enabled=true; CheckBox2->Enabled=true; ... CheckBox30->Enabled=true; this is obviously not a very efficient way of doing this... so I'm looking for a more efficient way... maybe something along the lines of for(int c=0;c!=31;c++) { CheckBox(c)->Enabled=true; } this code will obviously not work but any suggestions on how to make something like this work??? :~ Cheers* Debs

                  D Offline
                  D Offline
                  Debs
                  wrote on last edited by
                  #8

                  Thanks for all the suggestions I managed to do do it by the following::-D TCheckBox *ChkBx[34]; ChkBx[0]=CheckBox1; //Declaring them all once into an array ChkBx[1]=CheckBox2; ... ChkBx[33]=CheckBox33; for(int i=0;i!=4;i++) { ChkBx[i]->Checked=true; } I also used this concept with Graph Line Series ie. TChartSeries *Ser[4]; ... Thanks Again Debs -- modified at 13:11 Friday 23rd September, 2005

                  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