Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi all


I want to create buttons dynamically.

Example:If in the database 4 user present than it will create 4 buttons
after 10 second 5 more users inserted into database .
than it should create 14 button .if anybody deleted 2 user than i should create 12 button.

C#
for(int i=0;What to write;i++)
{
    Button btnTest = new Button();
}


I wish to change the text property outside the written for loop.

Is it possible?
If YES please write the code and If NO than suggest a process to Create 5 button dynamically without writing the Button btnTest = new Button(); syntax 5 time.


Please Help

Thanks in advance
Posted
Updated 18-Apr-12 2:44am
v3

Try:
C#
for(int i=0; i<5; i++)
    {
    Button btnTest = new Button();
    btnText.Text = i.ToString();
    btnTest.Location = new Point (i * 50, 50);
    Controls.Add(btnTest);
    }


[edit]Typo: "text" for "Text" - OriginalGriff[/edit]
 
Share this answer
 
v2
C#
for(int i=0;i<5;i++)
{
    Button btnTest = new Button();
    btnTest.Text = "whatever";
}


Read up on the button class.
 
Share this answer
 
Declare
C#
System.Windows.Forms.Button[] btnD = new System.Windows.Forms.Button[5];

and then
C#
for(int i=0;i<5;i++)
{
    btnD[i] = new Button();
     btnD[i].Text="Submit";
}


or you can provide name any where
C#
btnD[0].Text="Submit";
btnD[1].Text="Delete";
 
Share this answer
 
Comments
bhagirathimfs 18-Apr-12 10:26am    
Thanks

I want a button arraylist.
means i don't know the amount of button i want it is a variable and situation dependent so i can't write
Button[] btn = new Button[5];
so please suggest so that it will create infinite button not the fixed amount of data.
uspatel 19-Apr-12 0:34am    
Take input noofbuttons as integer
and provide size of arry dynamically.
int noofbuttons=10;
System.Windows.Forms.Button[] btnD = new System.Windows.Forms.Button[noofbuttons];
for(int i=0;i.btnD.Length;i++)
bhagirathimfs 19-Apr-12 2:04am    
Actually i want infinite buttons
How can i get this?

In the above query i will get only 10 buttons.
Please suggest

Thanks
uspatel 19-Apr-12 2:07am    
Do'nt confuse.
10 is not fixed.(Its only example)
you can Pass any number in noofbuttons variable.
Tell me/Share your code how and how many buttons you want to create.
Hi,
I have created a sample for you! try this:
C#
//I have a panel on the form
            //and controls will be added to it
            for (int i = 0; i < 5; i++)
            {
                string btnName = string.Format("Btn{0}", i.ToString());
                Button btn = new Button();
                btn.Name = btnName;
                btn.Left += i * 100; // you need to arrange them using some way
                panel1.Controls.Add(btn);
            }

            //Editing generated controls
            foreach (Control control in panel1.Controls)
            {
                if (control is Button)
                {
                    //Change Text and Color and ...
                    Button btn = (Button)control;
                    btn.Text = control.Name; // or everything you want
                    btn.Height = 20;
                    btn.Width = 100;
                }
            }


I hope it helps,
Cheers
 
Share this answer
 
Hi ,
Try this
C#
private void Form1_Load(object sender, EventArgs e)
      {
          for (int i =1; i < 5; i++)
          {
              Button button1 = new Button();

              button1.Location = new System.Drawing.Point(105 + 20 * i, 92 +20 * i);
              button1.Name = "button"+i;
              button1.Size = new System.Drawing.Size(75, 23  );
              button1.TabIndex = 0+i;
              button1.Text = "button" + i;
              button1.UseVisualStyleBackColor = true;
              this.Controls.Add(button1);
          }
      }

Best regards
M.Mitwalli
 
Share this answer
 
v2
Have you put any effort to this at all?

If only there where some property on the Button class that allowed one to set the Text of the Button. :rolleyes:
 
Share this answer
 
Comments
bhagirathimfs 18-Apr-12 8:32am    
Actually in my project there is no fix amount of button to create.
Means:sometime i need 1 button sometime 10 and sometime 100000 buttons .
So i put this question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900