Results 1 to 7 of 7

Thread: How do I create lots of controls faster in a dynamic form?

  1. #1

    Thread Starter
    Lively Member Christhemist's Avatar
    Join Date
    Sep 2016
    Location
    Nevada
    Posts
    116

    How do I create lots of controls faster in a dynamic form?

    I am looping through a SQL database of about 15 to 30 records and creating a set of about 50 controls per record, then populating each control with the correlating data. These controls range from textboxes, comboboxes, buttons, rich text boxes, checkboxes, labels, etc.

    However, this is taking very long to load and I cannot figure out a faster way to do this. Also, the application is locking up my computer when open.

    Any tips or advice would greatly be appreciated.

    Thanks in advance!

    Example of how I am creating and populating a control: (In my actual code there are a lot more controls inside the For Each Loop in the example below)

    Code:
    'Fill Datatable with data from SQL
    con.Open()
    DT.Clear()
    DTadapter = New SqlDataAdapter("SELECT * FROM TABLE", con)
    DTdapter.Fill(DT)
    
    Count = 0
    
    'Loop through DataTable and create controls for each record
     For Each row In DT.Rows
    
                '-----------Company
                Dim companyTB As New TextBox
                companyTB.Name = "companyTB" & Count
    
                With companyTB
    
                    .Size = New Size(200, 20)
                    companyTB.Font = New Font("Trebuchet MS", 9, FontStyle.Bold)
                    companyTB.BackColor = Color.LightYellow
    
                    If Count = 0 Then
                        .Location = New Point(10, 20)
                    Else
                        Y = Y + 460
                        .Location = New Point(10, Y)
                    End If
    
                End With
    	    
    	    'Add Control to Panel
                QC_Panel.Controls.Add(companyTB)
    
                'Populate Control with Data
                companyTB.Text = DT.Rows(Count)("COMPANY NAME").ToString
    
    Count = Count + 1
    
    Next
    
    Con.close()

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: How do I create lots of controls faster in a dynamic form?

    Quote Originally Posted by Christhemist View Post
    I am looping through a SQL database of about 15 to 30 records and creating a set of about 50 controls per record, then populating each control with the correlating data. These controls range from textboxes, comboboxes, buttons, rich text boxes, checkboxes, labels, etc.

    However, this is taking very long to load and I cannot figure out a faster way to do this. Also, the application is locking up my computer when open.

    Any tips or advice would greatly be appreciated.

    Thanks in advance!

    Example of how I am creating and populating a control: (In my actual code there are a lot more controls inside the For Each Loop in the example below)

    Code:
    'Fill Datatable with data from SQL
    con.Open()
    DT.Clear()
    DTadapter = New SqlDataAdapter("SELECT * FROM TABLE", con)
    DTdapter.Fill(DT)
    
    Count = 0
    
    'Loop through DataTable and create controls for each record
     For Each row In DT.Rows
    
                '-----------Company
                Dim companyTB As New TextBox
                companyTB.Name = "companyTB" & Count
    
                With companyTB
    
                    .Size = New Size(200, 20)
                    companyTB.Font = New Font("Trebuchet MS", 9, FontStyle.Bold)
                    companyTB.BackColor = Color.LightYellow
    
                    If Count = 0 Then
                        .Location = New Point(10, 20)
                    Else
                        Y = Y + 460
                        .Location = New Point(10, Y)
                    End If
    
                End With
    	    
    	    'Add Control to Panel
                QC_Panel.Controls.Add(companyTB)
    
                'Populate Control with Data
                companyTB.Text = DT.Rows(Count)("COMPANY NAME").ToString
    
    Count = Count + 1
    
    Next
    
    Con.close()
    A tip unrelated to your question but these lines can be shortened:
    Y = Y + 460
    Count = Count + 1
    to:
    Y += 460
    Count += 1

    As to your question, calling DoEvents should fix the freezing issue but will probably slow things down even more. This forum post appears to be related to your question: https://www.vbforums.com/showthread....r-of-controls).

    Does that help?

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: How do I create lots of controls faster in a dynamic form?

    Quote Originally Posted by Peter Swinkels View Post
    As to your question, calling DoEvents should fix the freezing issue but will probably slow things down even more.
    NO! Do NOT use DoEvents.... that's the last thing that you want... Remember two things: 1) DoEvents tells the system "hey, I'm not doing anything critical, so go take care of anything in the queue" ... and 2) UI updates are ALWAYS the lowest of priority ... so the UI updates will still take a backseat to anything else the system needs to do. What's taking so long is the rendering each time you add a control. So the solution is to NOT render anything until after you have added all the controls. Fortunately there's a way to do this: .SuspendLayout .... if you call the .SuspendLayout on the Panel BEFORE you start your loop .... and then call .ResumeLayout when you're done... it will only update it once, at the end, and after you've added all the controls... you should see a remarkable speed up in performance.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Lively Member Christhemist's Avatar
    Join Date
    Sep 2016
    Location
    Nevada
    Posts
    116

    Re: How do I create lots of controls faster in a dynamic form?

    I did as you recommended and it did not seem to make a difference whatsoever (see below for implementation). Sidenote: the form is freezing as well.

    Code:
    QC_Panel.SuspendLayout()
    Call CreateControls()
    QC_Panel.ResumeLayout()
    Last edited by Christhemist; Aug 3rd, 2020 at 03:49 PM.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How do I create lots of controls faster in a dynamic form?

    How about creating all the controls first and then adding them all with a single call to AddRange?

  6. #6
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: How do I create lots of controls faster in a dynamic form?

    Here's a quick fix. Add CompanyTB.Refresh after the CompanyTB.Text line.

    Explanation. You are only going to display a handful of text boxes at a time, even with a big Panel. Refresh forces each text box to be rendered straight away, instead of waiting for all the layout work to finish first (a curious habit of .Net framework). So the first few textboxes will be visible practically instantly.

    BB

  7. #7

    Thread Starter
    Lively Member Christhemist's Avatar
    Join Date
    Sep 2016
    Location
    Nevada
    Posts
    116

    Re: How do I create lots of controls faster in a dynamic form?

    This is what ended up working for me:

    Code:
    QC_Panel.SuspendLayout()
    Call GetData()
    Call CreateControls()
    QC_Panel.ResumeLayout()
    For whatever reason, separating out the SQL statements from the control creations speeded things up a lot.

    However, my form still locks up my computer up when it loses focus... I'm going to create a new forum post about this as I have not seen anything on the web that describes this issue. But if you would like to give me insight on this thread that would be great!

    Edit: new thread about form freezing: https://www.vbforums.com/showthread....81#post5489181

    Thanks for the help all!
    Last edited by Christhemist; Aug 4th, 2020 at 11:41 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width