troubleshooting Question

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

Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America asked on
.NET ProgrammingC#Windows OS
5 Comments1 Solution245 ViewsLast Modified:
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; }

       
    }
}

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();
        }
    }
}

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();
        }
    }
}
ASKER CERTIFIED SOLUTION
Mike Tomlinson
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Top Expert 2009

The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros