Create button during runtime in C#.net?

12,508

Solution 1

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 :)

Solution 2

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);
}

Solution 3

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.

Share:
12,508
HackTweaks
Author by

HackTweaks

Updated on June 04, 2022

Comments

  • HackTweaks
    HackTweaks almost 2 years

    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;

  • HackTweaks
    HackTweaks about 13 years
    do i really need button array? as @Anuraj have answered, I can just rename the button.
  • Jon Skeet
    Jon Skeet about 13 years
    @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?
  • HackTweaks
    HackTweaks about 13 years
    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
    HackTweaks about 13 years
    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...
  • Jon Skeet
    Jon Skeet about 13 years
    @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.
  • Anuraj
    Anuraj about 13 years
    @HackTweaks : I didn't get you :(