Avatar of Tom Knowlton
Tom Knowlton
Flag for United States of America asked on

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

.NET ProgrammingC#Windows OS

Avatar of undefined
Last Comment
Tom Knowlton

8/22/2022 - Mon
Mike Tomlinson

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 Knowlton

ASKER
Sorry - makes no difference.
ASKER CERTIFIED SOLUTION
Mike Tomlinson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Tom Knowlton

ASKER
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... : (
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Tom Knowlton

ASKER
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.