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 8762631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:34:47+00:00 2026-06-13T15:34:47+00:00

I want to create two dimensional array of buttons in windows form. I have

  • 0

I want to create two dimensional array of buttons in windows form. I have to do it with for loops because I want to have an array of 100 buttons. But I cant get it to work. I tried with List> and with Buttton[,] but doesn’t work. When I try like this:

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

        for (int y = 0; y < 5; y++)
        {
            this.tempList.Clear();

            for (int x = 0; x < 20; x++)
            {
                this.tempList.Add(new Button());
            }

            this.LEDarray.Add(tempList);
        }
        for (int y = 0; y < 5; y++)
        {
            for (int x = 0; x < 20; x++)
            {
                this.LEDarray[y][x].BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
                this.LEDarray[y][x].FlatStyle = System.Windows.Forms.FlatStyle.Flat;
                this.LEDarray[y][x].ForeColor = System.Drawing.Color.Black;

                xPos = xOffset + LEDsize * x + 20;
                yPos = yOffset + LEDsize * y + 20;

                this.LEDarray[y][x].Location = new System.Drawing.Point(xPos, yPos);
                this.LEDarray[y][x].Name = "button" + y.ToString() + x.ToString(); 
                this.LEDarray[y][x].Size = new System.Drawing.Size(LEDsize, LEDsize);
                this.LEDarray[y][x].TabIndex = 0;
                this.LEDarray[y][x].UseVisualStyleBackColor = false;
            }
        }

        for (int y = 0; y < 5; y++)
        {
            for (int x = 0; x < 20; x++)
            {
                this.Controls.Add(this.LEDarray[y][x]);
            }
        }

It only display the last row of buttons. So just fifth row and not the previous one. When I try similar with Button array it does not work at all. But array way is just SOS call. I want to do it with List so can you help me with code above to make it work?

  • 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-06-13T15:34:49+00:00Added an answer on June 13, 2026 at 3:34 pm

    Change condition for (int y = 0; y < 0; y++) to for (int y = 0; y < 5; y++) when adding buttons to controls.

    Second problem is that you are clearing your tempList, reference to which you just added to LEDarray. Create new list for each row of buttons:

    for (int y = 0; y < 5; y++)
    {
         // tempList.Clear() - this will remove all buttons from previous row
         tempList = new List<Button>();
    
         for (int x = 0; x < 20; x++)     
              tempList.Add(new Button());     
    
         LEDarray.Add(tempList);
    }
    

    Also I’d recommend you to increment TabIndex for buttons:

    this.LEDarray[y][x].TabIndex = 10 * y + x;
    

    And another advise – create your custom LEDButton class and use it:

    public class LEDButton : Button
    {
        public const int LEDWidth = 20;
        public const int LEDHeight = 20;
    
        public LEDButton()
        {
            BackColor = Color.FromArgb(0, 64, 0);
            ForeColor = Color.Black;
            FlatStyle = FlatStyle.Flat;
            Size = new Size(LEDWidth, LEDHeight);
            UseVisualStyleBackColor = false;
        }
    }
    

    Usage:

    // initialize array
    LEDButton[,] leds = new LEDButton[5, 20];
    for (int y = 0; y < leds.GetUpperBound(0); y++)
        for (int x = 0; x < leds.GetUpperBound(1); x++)
            leds[y, x] = new LEDButton()
                {
                    Name = String.Format("Button{0}{1}", y, x),
                    TabIndex = 10 * y + x,
                    Location = new Point(LEDButton.LEDWidth * x + 20,
                                         LEDButton.LEDHeight * y + 20)
                };
    // add buttons to controls
    for (int y = 0; y < leds.GetUpperBound(0); y++)
        for (int x = 0; x < leds.GetUpperBound(1); x++)
             Controls.Add(leds[y, x]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of two dimensional Arrays. I want to create a new
I want to create a two-dimensional array in which I want to store records
I want to create a two dimensional array dynamically. I know the number of
I need to create a two-dimensional array Class. I've done a work, but discovered
I have a two dimensional array with coordinates and i want to make the
I need to create a mutable two-dimensional array in Objective-C. For example I have:
I want to create a two dimensional array in a javascript function. I found
I have a simple one dimensional array but I want to split the long
I want to create two and three dimensional vectors using a constructor in a
I have a state column in a dataframe and I want to create two

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.