2

I know how to create button during runtime.

Button button1 = new Button();

button1.Location = new Point(20,10);
button1.Text = "Click Me";
// adding to groupBox1
groupBox1.Controls.Add(button1);

But the problem is i want to add multiple buttons like this..

for(int i = 1; i < 30; i++) {
Button button[i] = new Button();
// Button customization here...
...
groupBox1.Controls.Add(button[i]);

}

The code above is false code. How can I make this happen true in C#.net? i want to create multiple buttons with button name, button1, button2, button3, button4, .... button30;

2
  • What do you mean by false code? Code not able to compile? Commented Feb 9, 2011 at 9:47
  • I mean a pseudo prototype code! Commented Feb 9, 2011 at 10:16

5 Answers 5

4

You can't declare extra variables at execution time in C# - but you really don't want to anyway, as you wouldn't be able to access them dynamically afterwards. Just create an array:

// buttons would be declared as Button[] as a member variable
buttons = new Button[30]; 
for(int i = 0; i < buttons.Length; i++) {
    buttons[i] = new Button();
    // Button customization here...
    ...
    groupBox1.Controls.Add(buttons[i]);
}

Alternatively, use a List<Button>, which will certainly be more convenient if you don't know how many buttons you need beforehand. (See the obligatory "arrays considered somewhat harmful" blog post.)

Of course, if you don't actually need to get at the buttons later, don't bother assigning them to anything visible outside the loop:

for(int i = 0; i < 30; i++) {
    Button button = new Button();
    // Button customization here...
    ...
    groupBox1.Controls.Add(button);
}

You need to think about what information you need access to when... and how you want to access it. If you logically have a collection of buttons, you should use a collection type variable (like a list or an array).

Frankly I think it's one of the curses of the VS designers that you end up with horrible names such as "groupBox1" which carry no information beyond what's already in the type declaration, and encourage developers to think of collections of controls via individually-named variables. That's just me being grumpy though :)

Sign up to request clarification or add additional context in comments.

4 Comments

do i really need button array? as @Anuraj have answered, I can just rename the button.
@HackTweaks: I don't know whether you need the button array or not - I don't know what the rest of your code looks like! Do you need to access the buttons later or not?
I need to access the button only once. upon clicking on other manually created buttons, this buttons will be replaced by other one. And these button will be called whenever i click in the same loading button.
@HackTweaks: That's not entirely clear, but I'll assume you can sort it out. Basically, if you don't need to access the buttons outside the loop, keep them in a collection. If you don't need access, you don't need the collection.
1

Try this

for(int i = 1; i < 30; i++) {
Button button = new Button();
// Button customization here...
button.Name = "Button" + i.ToString();
groupBox1.Controls.Add(button);
}

2 Comments

As you said, i added the button with proper spacing. But what i actually wanted to do is create the button according to the number of row items in a DataTable. Since the space is limited, i want to load only 20 buttons at a time upon pressing same button/alternative button remaining 10 button will be added. I've set i<datatable.Rows.Count...
@HackTweaks : I didn't get you :(
1

You seem like you're almost on the right track:

// in form class
Button[] m_newButtons = new Button[30];

// in your trigger function
for(int i = 0; i < 30; ++i)
{
    m_newButtons[i] = new Button();
    // ...
    groupBox1.Controls.Add(m_newButtons[i]);
}

If you try and do this more than once you may have to remove the old buttons from the control before adding the new ones.

Comments

0
buttons = new Button[30]; 
for(int i = 0; i < buttons.Length; i++) {
    buttons[i] = new Button();

    groupBox1.Controls.Add(buttons[i]);
}

this code will work but button will be added one over other so set location also

buttons = new Button[30]; 
    for(int i = 0; i < buttons.Length; i++) 
    {
        buttons[i] = new Button();
        Point p=new Point(xvalue,yvalue);
       buttons[i].Location = p;       
        groupBox1.Controls.Add(buttons[i]);
    }

one thing you want to remember increment the x or y position by which you want to display it

Comments

0

Try this one out, I have just learned it myself:

public partial class Form1 : Form
{
 Button[] btn = new Button[12];// <--------<<<Button Array

 public Form1()
 {
  InitializeComponent();
 }   

 private void Form1_Load (object sender, EventArgs e)
 {    
    for (int i = 0; i < 12; i++)
    { 
     btn[i] = new Button ( ); 
     this.flowLayoutPanel1.Controls.Add(btn[i]);
    }
 }
 // double click on the flow layoutPannel initiates this code
 private void flowLayoutPanel1_Paint(object sender, PaintEventArgs e)
 {

 }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.