Way to speed this up? Adding custom UserControl to Controls Collection

Using C# while writing a Windows Form application.


I have a "main" UserControl which I intend to use for showing a collection of other UserControls.

Right now I am keeping it simple and just hard-coding the adding of the usercontrols to the "main" usercontrol.

I added 300 of the controls and they seemed to add okay.

But when I scroll the window for the "main" control there is a bunch of ... lag?  ... as i scroll.  The mouse pointer begins to move in slow motion.  My guess is that it is related to the "main" control re-drawing itself.  Once that process finishes, the mouse speed goes back to normal.

Tips?  Tricks?

I cannot believe that I am taxing the operating system THAT MUCH with my simple controls.  We're talking about bunch of textboxes, for crying out loud.

DataGridViews can have a lot more data and they scroll with no lag.  So what am i doing that is so different?

Frustrated that I am having to put up with this!  C# and Window Forms development has been around for a while.



code for adding my user controls to the "MAIN" control:

using System.Data;
using System.Windows.Forms;

namespace DocumentController
{
    public partial class InMemoryGrid : UserControl
    {
        public InMemoryGrid()
        {
            InitializeComponent();


            InMemoryRow imr;

            InMemoryCell imc;

            UniversalDataClass udc;

            int top = 0;

            for(int i = 0; i < 300; i++)
            {
                imr = new InMemoryRow();

                for(int j = 0; j < 10; j++)
                {
                    imc = new InMemoryCell();
                    udc = new UniversalDataClass() { RawObject = "Hello" };
                    imr.AddInMemoryControl(imc);

                    imr.Top = top;
                    imc.ShowOnly(WhichControl.textbox);
                    imc.SetValue(WhichControl.textbox, udc);
                }

                Controls.Add(imr);
                top += 50;
            }

           


        }

        public GlobalSpreadsheetRAWDataHelper gs { get; set; }

       
    }
}

Open in new window


code for the InMemoryRow usercontrol:
using System.Windows.Forms;

namespace DocumentController
{
    public partial class InMemoryRow : UserControl
    {
        public InMemoryRow()
        {
            InitializeComponent();
        }


        public void AddInMemoryControl(Control c)
        {
            this.SuspendLayout();
            tableLayoutPanelRow.SuspendLayout();
            tableLayoutPanelRow.Height = c.Height;
            this.Height = tableLayoutPanelRow.Height;
            tableLayoutPanelRow.Controls.Add(c);
            tableLayoutPanelRow.ColumnCount++;
            tableLayoutPanelRow.ResumeLayout();
            this.ResumeLayout();
        }
    }
}

Open in new window


Code for the InMrmoryCell:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DocumentController
{
    public enum WhichControl
    {
        textbox,
        combobox,
        checkbox,
        datetimepicker
    }
    public partial class InMemoryCell : UserControl
    {
        public InMemoryCell()
        {
            InitializeComponent();
        }

        public void ShowOnly(WhichControl wc)
        {
            this.SuspendLayout();
            textBoxERP.Visible = false;
            comboBoxERP.Visible = false;
            checkBoxERP.Visible = false;
            dateTimePickerERP.Visible = false;

            switch(wc)
            {
                case WhichControl.textbox:
                    textBoxERP.Visible = true;
                    break;
                case WhichControl.combobox:
                   comboBoxERP.Visible = true;
                    break;
                case WhichControl.checkbox:
                    checkBoxERP.Visible = true;
                    break;
                case WhichControl.datetimepicker:
                    dateTimePickerERP.Visible = true;
                    break;
                default:
            break;
            }

            this.ResumeLayout();
            
        }

        public void SetValue(WhichControl wc, UniversalDataClass udc)
        {
            this.SuspendLayout();
            switch(wc)
            {
                case WhichControl.textbox:
                    this.Height = textBoxERP.Height;
                    this.Width = textBoxERP.Width;
                    textBoxERP.Text = udc.AsTrimmedString;
                   // textBoxERP.Dock = DockStyle.Fill;                    
                    break;
                case WhichControl.combobox:
                    comboBoxERP.Visible = true;
                    break;
                case WhichControl.checkbox:
                    checkBoxERP.Visible = true;
                    break;
                case WhichControl.datetimepicker:
                    dateTimePickerERP.Visible = true;
                    break;
                default:
                    break;
            }
            this.ResumeLayout();
        }
    }
}

Open in new window

LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
First see if enabling double buffering on your control has any benefit:
    public partial class InMemoryGrid : UserControl
    {

        public InMemoryGrid()
        {
            InitializeComponent();
            this.DoubleBuffered = true;

            // ... rest of your code ...

        }

    }

Open in new window

Tom KnowltonWeb developerAuthor Commented:
Sorry - makes no difference.
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Does it only lag the first time you scroll to the bottom?  I'm wondering if it's a delayed usercontrol creation thing were the controls that are not visible don't fully initialize until they are scrolled into view causing lag.  Try scrolling all the way to the bottom, then back up, and see if it still lags when scrolling down a second time.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tom KnowltonWeb developerAuthor Commented:
Mike:

That was a good idea and I was excited to see if that was the case, but no, it still lags when I scroll, meaning, the mouse pointer slows to a crawl.  I have to wait for *whatever it is* to finish processing, then I can use the mouse as normal.  

I ran across this article:

https://codedump.io/share/78elZ5VKXBeD/1/fake-scrolling-containers-with-very-many-controls

I am making my way through it step by step.

Do you offer paid services?  For example, through Experts Exchange "Tech Help"?

To give you some context:  I am working as a lone contractor.  This piece of the project is due tomorrow.  If I meet with the company that is contracting with me and this is not finished ... I may be finished... : (
Tom KnowltonWeb developerAuthor Commented:
I eventually tabled this idea.  It was just taking too long to show the data.

I still need an answer, but will pursue this in a few months, not now.

For now...the information posted (including the link by me) is enough to smooth the way for the short run.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.