Click here to Skip to main content
16,021,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am building a winform Application. I have to create buttons dynamically based on data fetched from database. I followed following steps

Created UserControl
On UserControl_Load event I fetched data from database.
I populated the UserControl with buttons based on number of rows i get from the database.

The above approach proved too slow. The form loads slowly. It takes time to populate buttons. So Which is the best and optimised way to load controls dynamically in C# winforms?

C#
public ucFileCategoryPane()
        {
            InitializeComponent();
            PopulateNavPane();
        }

        public void PopulateNavPane()
        {
            tblFileController f = new tblFileController();
            List<tblFile> lfile = f.FileList();

            foreach (tblFile t in lfile)
            {
                vNavPaneItem vNavPaneItem2 = GetItemPanel(t);
                vnpFiles.Items.Add(vNavPaneItem2);
            }

            if (vnpFiles.Items.Count > 0)
            {
                string header = vnpFiles.Items[0].HeaderText;
                Connection.DbName = header;
                ListView li = CreateCategoryList(header);
                vnpFiles.Items[0].ItemPanel.Controls.Add(li);
            }
        }

        public vNavPaneItem GetItemPanel(tblFile t)
        {
            string ItemPanelName = "vip" + t.FileName;
            string ItemName = "vi" + t.FileName;
            string ItemText = t.FileName + " => Categories";
            vNavPaneItem vNavPaneItem2 = new vNavPaneItem();
            vNavPaneItem2.BackColor = System.Drawing.Color.White;
            vNavPaneItem2.HeaderText = t.FileName;
            vNavPaneItem2.ItemPanel.AutoScroll = true;
            vNavPaneItem2.ItemPanel.Location = new System.Drawing.Point(1, 30);
            vNavPaneItem2.ItemPanel.Name = ItemPanelName;
            vNavPaneItem2.ItemPanel.Size = new System.Drawing.Size(198, 168);
            vNavPaneItem2.ItemPanel.TabIndex = 1;
            vNavPaneItem2.Location = new System.Drawing.Point(0, 0);
            vNavPaneItem2.Name = ItemName;
            vNavPaneItem2.Size = new System.Drawing.Size(200, 199);
            vNavPaneItem2.TabIndex = 0;
            vNavPaneItem2.Text = ItemText;
            vNavPaneItem2.TooltipText = ItemText;
            vNavPaneItem2.Click += new EventHandler(vNavPaneItem2_Click);

            return vNavPaneItem2;
        }
Posted

1 solution

It appears to me that right now you are loading instances of a UserControl (?) named 'vNavPaneItem at run-time, not Buttons ... correct ? Or, is 'vNavPaneItem a sub-classed Button ?

In evaluating how you might speed up run-time loading, it's valuable to know if there is any prediction you can make ... for a given use of your application ... of how many Buttons or UserControls you will need to create.

If the range of possible variance of the number required is roughly predictable, you can try creating a set of the Buttons or UserControls at design-time, perhaps positioning them in advance (?), and then setting their Visible property to false. Then, as you query the database at run-time, you make the hidden Controls visible, and adjust their positions.

If you could be sure you are always going to need a certain minimum number of Buttons or UserControls, you could create those at design-time. Whether this strategy would really improve load time: maybe.

Clearly one needs to know more about your application's requirements and run-time behavior before pursuing this further, and testing possible improvement in Load time.

Have you done anything, yet, that tests to what extent it is the run-time creation of Controls that slows the application down ?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 30-Jan-14 13:18pm    
Good points, a 5. And I don't see anything fundamentally wrong with OP's way to populate controls. To put fairly simple things simply, I think the UI code should be, first of all neat, functional and good for maintenance; and only if performance is a real concern, think about more "optimized" ways.
-SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900