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

Tom Knowlton
Tom Knowlton used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
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 developer

Author

Commented:
Sorry - makes no difference.
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
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.
Tom KnowltonWeb developer

Author

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 developer

Author

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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial