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. How to add multiple controls at runTime ?

How to add multiple controls at runTime ?

Scheduled Pinned Locked Moved C#
helptutorialquestion
7 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.
  • H Offline
    H Offline
    hdv212
    wrote on last edited by
    #1

    Hi i want to add multiple controls to my form at runtime, for example, i have a single button on my form, when i click this button, the second button add to my form, again, when i click on first button, the third button add to my form and so on, this code is something like this :

    private void button3_Click(object sender, EventArgs e)
    {
    Button btn = new Button();
    btn.Location = new Point(this.button3.Location.X + 80, this.button3.Location.Y);
    this.Controls.Add(btn);
    }

    but the above code add only the first button at runTime and when i click again on button3 (in this example), the third button does not add to form. i think that it's couse by name of the controls which being create and it should have dynamic name for that control. can anybody has experience in this issue ? how to solve my problem ? Thanks

    U L 3 Replies Last reply
    0
    • H hdv212

      Hi i want to add multiple controls to my form at runtime, for example, i have a single button on my form, when i click this button, the second button add to my form, again, when i click on first button, the third button add to my form and so on, this code is something like this :

      private void button3_Click(object sender, EventArgs e)
      {
      Button btn = new Button();
      btn.Location = new Point(this.button3.Location.X + 80, this.button3.Location.Y);
      this.Controls.Add(btn);
      }

      but the above code add only the first button at runTime and when i click again on button3 (in this example), the third button does not add to form. i think that it's couse by name of the controls which being create and it should have dynamic name for that control. can anybody has experience in this issue ? how to solve my problem ? Thanks

      U Offline
      U Offline
      User 3491102
      wrote on last edited by
      #2

      Looks like your building buttons on top of each other. Isn't it always going to set the location to the same place for every button?

      1 Reply Last reply
      0
      • H hdv212

        Hi i want to add multiple controls to my form at runtime, for example, i have a single button on my form, when i click this button, the second button add to my form, again, when i click on first button, the third button add to my form and so on, this code is something like this :

        private void button3_Click(object sender, EventArgs e)
        {
        Button btn = new Button();
        btn.Location = new Point(this.button3.Location.X + 80, this.button3.Location.Y);
        this.Controls.Add(btn);
        }

        but the above code add only the first button at runTime and when i click again on button3 (in this example), the third button does not add to form. i think that it's couse by name of the controls which being create and it should have dynamic name for that control. can anybody has experience in this issue ? how to solve my problem ? Thanks

        U Offline
        U Offline
        User 3491102
        wrote on last edited by
        #3

        I would suggest: 1. Finding the previously added button and setting the new X relative to that through iteration/foreach. 2. Referencing the last control in the Controls list (might be the last one) using the length/count of the list.

        modified on Sunday, February 22, 2009 9:12 AM

        H 1 Reply Last reply
        0
        • U User 3491102

          I would suggest: 1. Finding the previously added button and setting the new X relative to that through iteration/foreach. 2. Referencing the last control in the Controls list (might be the last one) using the length/count of the list.

          modified on Sunday, February 22, 2009 9:12 AM

          H Offline
          H Offline
          hdv212
          wrote on last edited by
          #4

          Thanks Member 3493799 but i think beacuse the third control has a same name with second control, it does not create third control.

          D A 2 Replies Last reply
          0
          • H hdv212

            Thanks Member 3493799 but i think beacuse the third control has a same name with second control, it does not create third control.

            D Offline
            D Offline
            dan sh
            wrote on last edited by
            #5

            Set the name property different for each control added.

            The word "politics" describes the process so well: "Poli" in Latin meaning "many" and "tics" meaning "bloodsucking creatures." जय हिंद

            1 Reply Last reply
            0
            • H hdv212

              Thanks Member 3493799 but i think beacuse the third control has a same name with second control, it does not create third control.

              A Offline
              A Offline
              ABitSmart
              wrote on last edited by
              #6

              You are not giving the new button created a click handler.So it does not know what to do when clicked.

              1 Reply Last reply
              0
              • H hdv212

                Hi i want to add multiple controls to my form at runtime, for example, i have a single button on my form, when i click this button, the second button add to my form, again, when i click on first button, the third button add to my form and so on, this code is something like this :

                private void button3_Click(object sender, EventArgs e)
                {
                Button btn = new Button();
                btn.Location = new Point(this.button3.Location.X + 80, this.button3.Location.Y);
                this.Controls.Add(btn);
                }

                but the above code add only the first button at runTime and when i click again on button3 (in this example), the third button does not add to form. i think that it's couse by name of the controls which being create and it should have dynamic name for that control. can anybody has experience in this issue ? how to solve my problem ? Thanks

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

                Hi, every time you click button3 you are creating and adding a new button. That is fine, except: 1. all new buttons are at the same location, so you will only see one of them. 2. none of them will do anything when clicked, since you did not do any btn.Click+=... BTW: 1.the code to add controls at run-time can be identical to the code Visual Designer adds to your .Designer.cs file when adding a control at design-time. 2. the Name property of a Control is often not important at run-time; the Designer uses it at design-time. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                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