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. help with array of buttons

help with array of buttons

Scheduled Pinned Locked Moved C#
tutorialcsharpdata-structureshelp
6 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.
  • G Offline
    G Offline
    goldsoft
    wrote on last edited by
    #1

    hi i need to place on my form buttons as N number and i need that the buttons will be an array. for example: N=5 i need to see on screen: [button1] [button2] [button3] [button4] [button5] how to do it on C# WinForm FW3.5 thanks in advance

    P D 2 Replies Last reply
    0
    • G goldsoft

      hi i need to place on my form buttons as N number and i need that the buttons will be an array. for example: N=5 i need to see on screen: [button1] [button2] [button3] [button4] [button5] how to do it on C# WinForm FW3.5 thanks in advance

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Assuming that you have detailed your requirements correctly, and that you can't use a list (for example), you could do it like this (in pseudocode):

      declare array of size n

      for count = 0, count less than n
      create a new instance of button
      set the text of the button
      add it to the parent control collection (where parent could be a form, a panel, or the like)
      end for

      Forgive your enemies - it messes with their heads

      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

      My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

      P 1 Reply Last reply
      0
      • P Pete OHanlon

        Assuming that you have detailed your requirements correctly, and that you can't use a list (for example), you could do it like this (in pseudocode):

        declare array of size n

        for count = 0, count less than n
        create a new instance of button
        set the text of the button
        add it to the parent control collection (where parent could be a form, a panel, or the like)
        end for

        Forgive your enemies - it messes with their heads

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Don't forget to set the Location of the Button. I also tend to put have a Panel or GroupBox to hold such dynamic Controls to separate them from the non-dynamic items.

        1 Reply Last reply
        0
        • G goldsoft

          hi i need to place on my form buttons as N number and i need that the buttons will be an array. for example: N=5 i need to see on screen: [button1] [button2] [button3] [button4] [button5] how to do it on C# WinForm FW3.5 thanks in advance

          D Offline
          D Offline
          Dean Oliver
          wrote on last edited by
          #4

          The solution would be:

          public Form1()
          {
          InitializeComponent();

                 var buttons = new Button\[5\];
                 for (int i = 1; i < buttons.Length; i++)
                 {
                     buttons\[i\] = new Button();
                     buttons\[i\].Text = "\[button" + i + "\]";
                     buttons\[i\].Location = new Point(0, 25 \* i);
                 }
          
                 Controls.AddRange(buttons);
             }
          

          obviously you would have to adjust on your formatting. but thats the general idea. hope this helped.

          B 1 Reply Last reply
          0
          • D Dean Oliver

            The solution would be:

            public Form1()
            {
            InitializeComponent();

                   var buttons = new Button\[5\];
                   for (int i = 1; i < buttons.Length; i++)
                   {
                       buttons\[i\] = new Button();
                       buttons\[i\].Text = "\[button" + i + "\]";
                       buttons\[i\].Location = new Point(0, 25 \* i);
                   }
            
                   Controls.AddRange(buttons);
               }
            

            obviously you would have to adjust on your formatting. but thats the general idea. hope this helped.

            B Offline
            B Offline
            BillWoodruff
            wrote on last edited by
            #5

            Please note the above code would produce #4 buttons, not #5. The array element at position #0 will be undefined. If the button's width, which you don't specify, is exactly #25, then they will be equally spaced, and not overlap; but that makes an assumption about a button's default width, which can be affected by whether 'AutoSize is 'true, and the width of the 'Text property content. And, no, I did not down-vote your post. best, Bill

            "Science is facts; just as houses are made of stones: so, is science made of facts. But, a pile of stones is not a house, and a collection of facts is not, necessarily, science." Henri Poincare

            D 1 Reply Last reply
            0
            • B BillWoodruff

              Please note the above code would produce #4 buttons, not #5. The array element at position #0 will be undefined. If the button's width, which you don't specify, is exactly #25, then they will be equally spaced, and not overlap; but that makes an assumption about a button's default width, which can be affected by whether 'AutoSize is 'true, and the width of the 'Text property content. And, no, I did not down-vote your post. best, Bill

              "Science is facts; just as houses are made of stones: so, is science made of facts. But, a pile of stones is not a house, and a collection of facts is not, necessarily, science." Henri Poincare

              D Offline
              D Offline
              Dean Oliver
              wrote on last edited by
              #6

              I agree with your statement. This was a rough general idea so he could get started. Not a final solution. Please bare that in mind. Thanks though.

              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