Phil
Phil

Reputation: 627

Why is my WinForms panel's scrollbars freezing the app?

I only have 2,000 32wx32h PictureBoxes within the panel in a grid-like fashion. I thought that was the issue, so I set it to 1,250 PictureBoxes (50w25h), and I'm getting the same problem.

This is not a real-time event loop. It's just a bunch of controls side by side waiting on events.

What I do is scroll down and up.. works fine. Left and right... works fine. After some scrolling for 1-2 minutes, it freezes up the entire app.

My panel's AutoScroll is set to True. None of my own code operates on the scrollbars. All it does is scroll a billion PictureBoxes around.

And I noted that disabling the scrollbars does not freeze the app later, so I'm figuring it's a scrollbar issue.

The debugger is giving no information.

Process Memory says 17.9mb is being used.

It seems to be always be reproducible with these following steps: Scroll the vertical bar down somewhat, such as half way down, then let go of mouse button, move up toward the red 'X' at the top-right of the window, and it freezes.

Other times it's just moving the vertical and horizontal scrollbars around for 30 seconds to 2 minutes, and it randomly freezes.

New Note: During a freeze, I noticed in about 5-10 seconds later the scrollbars can be moved again. Then it's quick to go back into a short freeze and repeat.

On load, this is used for initializing the grid:

        // Initialize a blank map 
        for (int row = 0; row < m_mapInfo.Rows; row++)
        {
            for (int col = 0; col < m_mapInfo.Columns; col++)
            {
                PictureBox pb = new PictureBox();
                pb.Size = size;
                Point loc = new Point(m_mapInfo.TileWidth * col, m_mapInfo.TileHeight * row);
                Rectangle srcRect = new Rectangle(loc, size);
                pb.Location = loc;
                pb.Tag = -1;
                pb.Name = String.Format("Col={0:00}-Row={1:00}", col, row);
                pb.BackColor = Color.Gray;
                pb.MouseDown += pbMap_MouseDown;
                pb.MouseUp += pbMap_MouseUp;    
                pnlMap.Controls.Add(pb);
            }
        }

Upvotes: 0

Views: 1546

Answers (2)

Shift Technologies
Shift Technologies

Reputation: 11

The problem is in AutoScroll. I have experienced this in one of my music player projects in 2016 too. The best way I found to get rid of this issue was:

  1. Add all your picturebox to a control like panel.
  2. Don't dock the panel and set AutoScroll to False
  3. Set the height of the panel to the (location.y of last picturebox + height of picturebox)(you can increase the height according to your UI needs)
  4. Now use a vertical scrollbar to move the panel.top.

Upvotes: 1

Phil
Phil

Reputation: 627

The hanging appears to be a bug while in debug mode in Visual Studio 2017. My solution was to maximize the window and panel and not use the scrollbars. If I have my window maxed initially without the use of scrollbars, it works fine with excellent performance. If I reduce the window to its original size and use the scrollbars, it hangs the application within 30 seconds - 2 minutes. Both of these cases have been tested with the same amount of PictureBoxes.

Upvotes: 0

Related Questions