We help IT Professionals succeed at work.
Get Started

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

244 Views
Last Modified: 2017-12-08
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
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE