Show tab with lots of ComboBox controls slowly with WinForms

I set up a dialog with several tabs. One of them contains twenty combo boxes, each of which contains more than 100 items, added as follows:

foreach (var x in collection)
{
    string text = FormatItem (x);
    combo.Items.Add (text);
}

therefore there is nothing unusual in the details. These are simple lines, and the combo boxes are populated when creating the dialog. This happens almost instantly.

However, when the user clicks on the tab containing all of these combined fields for the first time, the GUI freezes for several seconds (and I'm running on a really muscular machine).

I downloaded the symbols for System.Windows.Formsand tried to break into the debugger when the program got stuck. I found a stack trace with the following calls:

System.Windows.Forms.Control.CreateHandle()
System.Windows.Forms.ComboBox.CreateHandle()
System.Windows.Forms.Control.CreateControl(...) x 3
System.Windows.Forms.Control.SetVisibleCore(true)
System.Windows.Forms.TabPage.Visible.set(true)

, WndProc .. , . .

, WinForms. , , - , ? ?

Nota bene:

  • , , .

  • Handle , , . , . .

  • BeginUpdate EndUpdate : , , . , .

+3
5

, , , . .

- , , , danbystrom, Items, . , ( BeginUpdate EndUpdate), ( 200 ).

+1

, , , - ...: s

.BeginUpdate/.EndUpdate?

, , - , . , , ... ( , , / .)

+1

ComboBox.DataSource ?

comboBox1.DataSource = myCollection1;
comboBox2.DataSource = myCollection2;
comboBox3.DataSource = myCollection3;
// and so on...

:

public class Entity
    {
        public string Title { get; set; }
        public override string ToString()
        {
            return Title;
        }
    }
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            List<Entity> list = new List<Entity>
                                    {
                                        new Entity {Title = "Item1"},
                                        new Entity {Title = "Item2"},
                                        new Entity {Title = "Item3"}
                                    };

            comboBox1.DataSource = list;

        }
+1

. - , 50-100 . .

, datagrid. , . , , .

0

, 4000 . .

OnLoad , , , InitializeComponent(), .

, OnLoad , , ? , , , - .

0

Source: https://habr.com/ru/post/1703726/


All Articles