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()
Re: How do I create lots of controls faster in a dynamic form?
Quote:
Originally Posted by
Christhemist
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?
Re: How do I create lots of controls faster in a dynamic form?
Quote:
Originally Posted by
Peter Swinkels
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
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()
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?
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
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!