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. Dynamic Button Loop

Dynamic Button Loop

Scheduled Pinned Locked Moved C#
tutorialquestion
10 Posts 5 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.
  • M Offline
    M Offline
    Michael Fritzius
    wrote on last edited by
    #1

    Hello, I'm trying to make a program where I modify properties of a lot of buttons. The names are sequential, like button1, button2, etc. all the way to button81. Buttons 82 through 90, though, I don't want to mess with--they have a different purpose. I can't remember how to do this and I definitely don't want to modify each one individually. Any pointers? Thanks for your time, Michael Fritzius

    G H M 3 Replies Last reply
    0
    • M Michael Fritzius

      Hello, I'm trying to make a program where I modify properties of a lot of buttons. The names are sequential, like button1, button2, etc. all the way to button81. Buttons 82 through 90, though, I don't want to mess with--they have a different purpose. I can't remember how to do this and I definitely don't want to modify each one individually. Any pointers? Thanks for your time, Michael Fritzius

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      for(int i=1; i<91;i++) { this.Controls["button"+i].Text="this is button number "+i.ToString(); }

      #region signature my articles #endregion

      P 1 Reply Last reply
      0
      • M Michael Fritzius

        Hello, I'm trying to make a program where I modify properties of a lot of buttons. The names are sequential, like button1, button2, etc. all the way to button81. Buttons 82 through 90, though, I don't want to mess with--they have a different purpose. I can't remember how to do this and I definitely don't want to modify each one individually. Any pointers? Thanks for your time, Michael Fritzius

        H Offline
        H Offline
        Hesham Amin
        wrote on last edited by
        #3

        I think you should create them dynamically at runtime.


        Hesham A. Amin My blog

        1 Reply Last reply
        0
        • G Giorgi Dalakishvili

          for(int i=1; i<91;i++) { this.Controls["button"+i].Text="this is button number "+i.ToString(); }

          #region signature my articles #endregion

          P Offline
          P Offline
          Paul Conrad
          wrote on last edited by
          #4

          It would work fine, but he doesn't want to mess with buttons 82 through 90.

          "I really like comments where I don't have to answer stupid questions" - stfx

          G 1 Reply Last reply
          0
          • P Paul Conrad

            It would work fine, but he doesn't want to mess with buttons 82 through 90.

            "I really like comments where I don't have to answer stupid questions" - stfx

            G Offline
            G Offline
            Giorgi Dalakishvili
            wrote on last edited by
            #5

            Oh I forgot then. Then it will look like this: for(int i=1; i<82;i++) { this.Controls["button"+i].Text="this is button number "+i.ToString(); }

            #region signature my articles #endregion

            P 1 Reply Last reply
            0
            • G Giorgi Dalakishvili

              Oh I forgot then. Then it will look like this: for(int i=1; i<82;i++) { this.Controls["button"+i].Text="this is button number "+i.ToString(); }

              #region signature my articles #endregion

              P Offline
              P Offline
              Paul Conrad
              wrote on last edited by
              #6

              Could always throw in a switch/if block if there is a range within the loop he wants to skip :-D

              "I really like comments where I don't have to answer stupid questions" - stfx

              A 1 Reply Last reply
              0
              • P Paul Conrad

                Could always throw in a switch/if block if there is a range within the loop he wants to skip :-D

                "I really like comments where I don't have to answer stupid questions" - stfx

                A Offline
                A Offline
                Anthony Mushrow
                wrote on last edited by
                #7

                Look, if you want to skip a small portion of a loop, and an if statement, and continue:

                for(int i=0; i<100; i++) {

                if(i >= 82 && i <= 90)
                    continue;
                
                this.Controls\[whatever\]...
                

                }

                My current favourite word is: Waffle Cheese is still good though.

                P 1 Reply Last reply
                0
                • A Anthony Mushrow

                  Look, if you want to skip a small portion of a loop, and an if statement, and continue:

                  for(int i=0; i<100; i++) {

                  if(i >= 82 && i <= 90)
                      continue;
                  
                  this.Controls\[whatever\]...
                  

                  }

                  My current favourite word is: Waffle Cheese is still good though.

                  P Offline
                  P Offline
                  Paul Conrad
                  wrote on last edited by
                  #8

                  Yeah, there are alot of ways we could get creative with it :-D

                  "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                  A 1 Reply Last reply
                  0
                  • P Paul Conrad

                    Yeah, there are alot of ways we could get creative with it :-D

                    "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                    A Offline
                    A Offline
                    Anthony Mushrow
                    wrote on last edited by
                    #9

                    You could do all sorts of things. I still like the idea of creating the buttons dynamically though. Of course then if you needed to change them, hmm.

                    My current favourite word is: Waffle Cheese is still good though.

                    1 Reply Last reply
                    0
                    • M Michael Fritzius

                      Hello, I'm trying to make a program where I modify properties of a lot of buttons. The names are sequential, like button1, button2, etc. all the way to button81. Buttons 82 through 90, though, I don't want to mess with--they have a different purpose. I can't remember how to do this and I definitely don't want to modify each one individually. Any pointers? Thanks for your time, Michael Fritzius

                      M Offline
                      M Offline
                      Michael Fritzius
                      wrote on last edited by
                      #10

                      Wow I haven't gotten the chance to come back and say thanks to everyone who gave answers. But I took the advice of the first person, which is what I needed, because I need to still modify the buttons during the program. Thanks to all! Michael Fritzius

                      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