Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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.
 
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 18-Apr-12 3:12am
Edited 18-Apr-12 3:44am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try:
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]
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

for(int i=0;i<5;i++)
{
    Button btnTest = new Button();
    btnTest.Text = "whatever";
}
 
Read up on the button class.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Declare
System.Windows.Forms.Button[] btnD = new System.Windows.Forms.Button[5];
and then
 
for(int i=0;i<5;i++)
{
    btnD[i] = new Button();
     btnD[i].Text="Submit";
}
 
or you can provide name any where
btnD[0].Text="Submit";
btnD[1].Text="Delete";
  Permalink  
Comments
bhagirathimfs at 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.
Uma Shankar Patel at 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 at 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
Uma Shankar Patel at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Hi,
I have created a sample for you! try this:
//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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Hi ,
Try this
  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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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. Roll eyes | :rolleyes:
  Permalink  
Comments
bhagirathimfs at 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)

  Print Answers RSS
0 OriginalGriff 280
1 Sergey Alexandrovich Kryukov 279
2 CPallini 205
3 Maciej Los 197
4 Afzaal Ahmad Zeeshan 160
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 18 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100