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

  • SEARCH
  • Home
  • 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 6797957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:40:21+00:00 2026-05-26T18:40:21+00:00

I am trying to assign button controls text dynamically by using a loop like

  • 0

I am trying to assign button controls text dynamically by using a loop like the following:

int ButtonNumber = 1;

while (ButtonNumber < 10)
{
    //WHAT TO DO HERE!?
    ButtonNumber++;
}

I wanna avoid the following:

button1.Text = "";
button2.Text = "";
button3.Text = "";
button4.Text = "";
button5.Text = "";
button6.Text = "";
button7.Text = "";
button8.Text = "";
button9.Text = "";
  • 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-26T18:40:22+00:00Added an answer on May 26, 2026 at 6:40 pm

    Ideally, don’t have button1, button2 etc as variables in the first place. Have a collection (e.g. a List<Button>):

    private List<Button> buttons = new List<Button>();
    

    (EDIT: You’d need to populate this somewhere, of course…)

    then to update the text later:

    for (int i = 0; i < buttons.Count; i++)
    {
        buttons[i].Text = "I'm button " + i;
    }
    

    This doesn’t play terribly nicely with the designer, unfortunately.

    You can fetch a control by ID, but personally I try not to do that. If you really want to:

    for (int i = 1; i <= 10; i++)
    {
        Control control = Controls["button" + i];
        control.Text = "I'm button " + i;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a button using jquery. I use the following code
I am trying to assign each button on the left its own Tab control.
I'm trying to assign a variable from a html text input value and then
I'm trying to dynamically assign a different instance of jQuery-UI's resizable after the DOM
I am trying to add a button inside a jqGrid column and to assign
I am trying to scrape form field IDs using Beautiful Soup like this for
I am trying to assign a tag to button. The normal command is: button.tag
I am trying to dynamically create controls and give them properties during run time.
I'm simply trying to assign a UITextField.text to an NSString in my object. The
I am trying to assign the onclick event to an anchor tag that already

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.