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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:11:49+00:00 2026-05-23T16:11:49+00:00

I am creating a panel and then adding some labels/buttons to it to form

  • 0

I am creating a panel and then adding some labels/buttons to it to form a grid. The issue is that if I add more than say 25×25 items to the panel, there is a terrible performance hit. I can resize the form ok but when I scroll the panel to see all the labels the program lags, the labels/buttons tear or flicker, and sometimes it can make the program unresponsive. I have tried adding the controls to a “DoubleBufferedPanel” that I created. This seems to have no effect. What else could I do? Sorry for such a large code listing. I didn’t want to waste anyone’s time.

namespace GridTest
{
    public partial class Form1 : Form
    {
        private const int COUNT = 50;
        private const int SIZE = 50;
        private Button[,] buttons = new Button[COUNT, COUNT];
        private GridPanel pnlGrid;

        public Form1()
        {
            InitializeComponent();

            pnlGrid = new GridPanel();
            pnlGrid.AutoScroll = true;
            pnlGrid.Dock = DockStyle.Fill;
            pnlGrid.BackColor = Color.Black;

            this.Controls.Add(pnlGrid);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int x = 0;
            int y = 0;
            int offset = 1;

            for (int i = 0; i < COUNT; i++)
            {
                for (int j = 0; j < COUNT; j++)
                {
                    buttons[i, j] = new Button();
                    buttons[i, j].Size = new Size(SIZE, SIZE);
                    buttons[i, j].Location = new Point(x, y);
                    buttons[i, j].BackColor = Color.White;

                    pnlGrid.Controls.Add(buttons[i, j]);

                    x = x + SIZE + offset;
                }

                x = 0;
                y = y + SIZE + offset;
            }
        }
    }
}

Also, the GridPanel class:

namespace GridTest
{
    public class GridPanel : Panel
    {
        public GridPanel()
            : base()
        {
            this.DoubleBuffered = true;
            this.ResizeRedraw = false;
        }
    }
}
  • 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-23T16:11:50+00:00Added an answer on May 23, 2026 at 4:11 pm

    If you must add controls at run time, based on some dynamic or changing value, you might want to consider creating an image on the fly, and capturing mouse click events on its picturebox. This would be much quicker and only have one control to draw rather than hundreds. You would lose some button functionality such as the click animation and other automatic properties and events; but you could recreate most of those in the generation of the image.

    This is a technique I use to offer users the ability to turn on and off individual devices among a pool of thousands, when the location in a 2-dimensional space matters. If the arrangement of the buttons is unimportant, you might be better offering a list of items in a listview or combobox, or as other answers suggest, a datagridview with button columns.

    EDIT:

    An example showing how to add a graphic with virtual buttons. Very basic implementation, but hopefully you will get the idea:

    First, some initial variables as preferences:

    int GraphicWidth = 300;
    int GraphicHeight = 100;
    int ButtonWidth = 60;
    int ButtonHeight = 20;
    Font ButtonFont = new Font("Arial", 10F);
    Pen ButtonBorderColor = new Pen(Color.Black);
    Brush ButtonTextColor = new SolidBrush(Color.Black);
    

    Generating the image:

    Bitmap ControlImage = new Bitmap(GraphicWidth, GraphicHeight);
    using (Graphics g = Graphics.FromImage(ControlImage))
    {
        g.Clear(Color.White);
        for (int x = 0; x < GraphicWidth; x += ButtonWidth)
            for (int y = 0; y < GraphicHeight; y += ButtonHeight)
            {
                g.DrawRectangle(ButtonBorderColor, x, y, ButtonWidth, ButtonHeight);
                string ButtonLabel = ((GraphicWidth / ButtonWidth) * (y / ButtonHeight) + x / ButtonWidth).ToString();
                SizeF ButtonLabelSize = g.MeasureString(ButtonLabel, ButtonFont);
                g.DrawString(ButtonLabel, ButtonFont, ButtonTextColor, x + (ButtonWidth/2) - (ButtonLabelSize.Width / 2), y + (ButtonHeight/2)-(ButtonLabelSize.Height / 2));
            }
    }
    pictureBox1.Image = ControlImage;
    

    And responding to the Click event of the pictureBox:

    // Determine which "button" was clicked
    MouseEventArgs em = (MouseEventArgs)e;
    Point ClickLocation = new Point(em.X, em.Y);
    int ButtonNumber = (GraphicWidth / ButtonWidth) * (ClickLocation.Y / ButtonHeight) + (ClickLocation.X / ButtonWidth);
    MessageBox.Show(ButtonNumber.ToString());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a multi-part web form in ASP.NET that uses Panels for the different
I have a form with a panel docked in it. I then dynamically create
I'm creating a custom drop down list with AJAX dropdownextender. Inside my drop panel
I'm creating a set of search panes that allow users to tweak their results
I'm creating eBay style search panes that allow users to narrow their results set
Creating hashes of hashes in Ruby allows for convenient two (or more) dimensional lookups.
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
I'm creating a dashboard application that shows hundreds of items on a FlowLayoutPanel .
I'm trying to extend a class like panel so that I can fire click
I am creating a small game, the game is printed onto a panel on

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.