Adding buttons to the form dynamically

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kinannawaz
    New Member
    • Dec 2007
    • 20

    #1

    Adding buttons to the form dynamically

    HELP NEEDED IN C# SHARP??

    I have been working on a project in c sharp i am trying to add buttons to the panel in my form when the user clicks a button "add more".

    How will i do that add buttons to the panel in the form dynamically that (new button) should be placed on nextline.
  • vanc
    Recognized Expert New Member
    • Mar 2007
    • 211

    #2
    Originally posted by kinannawaz
    HELP NEEDED IN C# SHARP??

    I have been working on a project in c sharp i am trying to add buttons to the panel in my form when the user clicks a button "add more".

    How will i do that add buttons to the panel in the form dynamically that (new button) should be placed on nextline.
    Button aButton = new Button();
    aButton.Name = "Button"; //you also can set height width
    targetPanel.Con trols.Add(aButt on);

    Cheers.

    Comment

    • kinannawaz
      New Member
      • Dec 2007
      • 20

      #3
      Originally posted by vanc
      Button aButton = new Button();
      aButton.Name = "Button"; //you also can set height width
      targetPanel.Con trols.Add(aButt on);

      Cheers.

      You r right but this only creates one button dynamically
      I want to create buttons as long as user wants
      I am posting my code that will add a textbox and a label to the panel of the form
      It only prints one textbox and and label dynamically

      private void button1_Click(o bject sender, EventArgs e)
      {
      j = j + 25;// increment the position of the labael and text box
      this.textbox2.S ize = new System.Drawing. Size(75, 20);
      this.textbox2.L ocation = new System.Drawing. Point(91, j);
      this.textbox2.N ame.Equals("tex tbox" + i);

      this.label2.Aut oSize = true;
      this.label2.Loc ation = new System.Drawing. Point(13, j);
      this.label2.Nam e.Equals("Enter number" + i);
      this.label2.Siz e = new System.Drawing. Size(72, 13);
      this.label2.Tex t = "Enter Number" + i;

      this.panel1.Con trols.Add(this. textbox2);
      this.panel1.Con trols.Add(this. label2);
      i++;//increments the number of textbox an label added
      }

      [IMG]C:\Documents and Settings\NOUMAN \Desktop\untitl ed.bmp[/IMG]

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        What do you mean only creates one button dynamically?
        It will make one button dynamically as many times as you call that code, for example on every button click.

        I see your code does not actually create a new textbox and label but mearly move around some existing ones. Be sure to actually create NEW instances of the controls you want.

        Comment

        • kinannawaz
          New Member
          • Dec 2007
          • 20

          #5
          Originally posted by Plater
          What do you mean only creates one button dynamically?
          It will make one button dynamically as many times as you call that code, for example on every button click.

          I see your code does not actually create a new textbox and label but mearly move around some existing ones. Be sure to actually create NEW instances of the controls you want.

          you r right when i run the program only the position of the label and textbox changes of that particular textbox and label nothing else .

          the same textbox and label moves down the panel but nothing is added to the panel

          how do i declare and initialize label and text box dynamically so that every time a button is clicked it declares and initialize a new textbox and label and adds it to the panel contol......
          thx

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            It is shown in the code for adding a button given prior.
            Code:
            Textbox temptb = new TextBox();
            Label templb = new Label();
            //do stuff with them.
            //like setting locations and whatnot
            
            //then add them to your form
            this.Controls.Add(temptb);
            this.Controls.Add(templb);

            Comment

            • kinannawaz
              New Member
              • Dec 2007
              • 20

              #7
              Originally posted by Plater
              It is shown in the code for adding a button given prior.
              Code:
              Textbox temptb = new TextBox();
              Label templb = new Label();
              //do stuff with them.
              //like setting locations and whatnot
              
              //then add them to your form
              this.Controls.Add(temptb);
              this.Controls.Add(templb);

              THANKS PLATER IT REALLY WORKED

              THANK YOU

              THANK YOU

              Comment

              • kinannawaz
                New Member
                • Dec 2007
                • 20

                #8
                Originally posted by Plater
                It is shown in the code for adding a button given prior.
                Code:
                Textbox temptb = new TextBox();
                Label templb = new Label();
                //do stuff with them.
                //like setting locations and whatnot
                
                //then add them to your form
                this.Controls.Add(temptb);
                this.Controls.Add(templb);

                Thanks it worked my new modified function

                private void call()
                {
                TextBox textbox1=new TextBox();
                Label label1= new Label();

                j = j + 25;
                textbox1.Size = new System.Drawing. Size(75, 20);
                textbox1.Locati on = new System.Drawing. Point(91, j);
                textbox1.Name.E quals("textbox" + i);

                label1.AutoSize = true;
                label1.Location = new System.Drawing. Point(13, j);
                label1.Name.Equ als("Enter number" + i);
                label1.Size = new System.Drawing. Size(72, 13);
                label1.Text = "Enter Number" + i;

                this.panel1.Con trols.Add(textb ox1);
                this.panel1.Con trols.Add(label 1);
                i++;
                }

                The textboxes and labels works properly that are dynamically created


                How do i get the values from these dynamically created textboxes when user clicks a button??


                thx

                Comment

                • kinannawaz
                  New Member
                  • Dec 2007
                  • 20

                  #9
                  Here is the new code i have done to get the values from the dynamically created textboxes.
                  Every time this function is called a new textbox and label is added to the panel of my form.
                  How do i get the values from these dynamically creadted textboxes.
                  I have used an array to get the values but the vakues are not fetched from the textbox.

                  void create_label_te xtbox()
                  {
                  TextBox textBox1 = new TextBox();
                  Label label1 = new Label();

                  j = j + 25;
                  textBox1.Size = new System.Drawing. Size(75, 20);
                  textBox1.Locati on = new System.Drawing. Point(91, j);
                  textBox1.Name.E quals("textbox" + i);

                  label1.AutoSize = true;
                  label1.Location = new System.Drawing. Point(13, j);
                  label1.Name.Equ als("Enter number" + i);
                  label1.Size = new System.Drawing. Size(72, 13);
                  label1.Text = "Enter Number" + i;

                  this.panel1.Con trols.Add(textB ox1);
                  this.panel1.Con trols.Add(label 1);

                  //this is an error the value are not fetched from the text box how
                  // do i fetch values from the dynamically created textbox
                  array[i]=int.Parse(text Box1.Text.ToStr ing
                  i++;
                  }

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    Loop through the form's Controls collection for the .Name property of the control you want.

                    Comment

                    • kinannawaz
                      New Member
                      • Dec 2007
                      • 20

                      #11
                      I have made a new function to solve the problem to get the dynamically created controls from the windows forms and the panel inside the form but i am having a problem in it please help

                      public void call2()
                      {
                      string str="";
                      //Form.ControlCol lection formControls = new Form.ControlCol lection(Form3);

                      foreach (Control c in Controls)
                      {
                      if (c is TextBox)
                      str += c.Name;
                      else if (c is Label)
                      str += c.Name;
                      else if (c is Panel)
                      {
                      //get the control inside the panel

                      foreach (Control d in c) //it is an error as it does not allow me to do so
                      {
                      if (c is TextBox)
                      str += c.Name;
                      else
                      str += c.Name;
                      }
                      }
                      else if (c is Label)
                      str += c.Name;
                      else
                      str += c.Name;

                      }
                      MessageBox.Show (str);
                      }

                      Comment

                      • Plater
                        Recognized Expert Expert
                        • Apr 2007
                        • 7872

                        #12
                        Code:
                        foreach (Control d in c) //it is an error as it does not allow me to do so
                        c is a control, not a collection of controls, try c.Controls instead.

                        Also, you only allow for one extra level of children. What if the control you want is a child of a child of the main control? Like a textbox inside a groupbox inside another groupbox?

                        Comment

                        Working...