Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 386685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:36:01+00:00 2026-05-12T15:36:01+00:00

I have 16 buttons named button1, button2… etc What is the best way to

  • 0

I have 16 buttons named button1, button2… etc
What is the best way to iterate them and How do I need to store these buttons in( in an array or something)? I’d like to do something like the below:

public void func(string[] a)
{
     for(int i = 0; i<16;i++)
     {
          if(a[i]==something)
              button[i].image = someImage;
          else
              button[i].image = antoherImage;
     }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T15:36:01+00:00Added an answer on May 12, 2026 at 3:36 pm

    The best thing is frankly not to have 16 different variables to start with. Instead, have an array:

    Button[] buttons = new Button[16];
    

    Then you can use exactly the syntax you want (modulo the property name). The disadvantage is that the designer doesn’t really cater for this. Normally I find that if I need a lot of UI elements of the same type, it’s easier to create them programmatically than in the designer, but it depends on the situation.

    Another option is to use Controls["button" + (i + 1)], but that feels like a bit of a hack to me when you really just want an array. If you don’t ever need to reassign the variables, you could use:

    Button[] buttons = new Button[16];
    for (int i = 0; i < 16; i++)
    {
        buttons[i] = (Button) Controls["button" + (i + 1)];
    }
    

    and from then on you can use the buttons array…

    Of course you could do this instead:

    Button[] buttons = { button1, button2, button3, button4, button5, ... };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of buttons named: button1 button2 button3 etc. Is there a
Suppose I have an application that have similar buttons named button0, button1, etc, up
I have a main form (frmMain) , with some buttons (button1, button2...) . then
I have 5 buttons named opt1, opt2, opt3, etc. If I want to hide/show/do
I have two buttons, and need to link them with a line. I bind
I have some 10 buttons with different names each.On selecting each button I need
i have create 5 dynamic buttons in 1 row and store the button info
I have multiple buttons on a single page. I need when I click a
I have created two buttons using the following XAML code. <Button x:Name=Button1 Width=100 Content=Button1
I have html like this: <html> <input type='button' name='button1' id='button1'> <input type='button' name='button2' id='button2'>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.