i have a questionnaire like formular with (depends on configuration) 3 to n pages realized with a tab control. The instantiation is horribly slow, but thats surely caused by the control count that can easily reach 100 (f.e. 13 questions with 1 label and 7 radio buttons each) on a single page.
To speed things up i thought about creating the first page only and when the form is visible and the user is working on the first page then dynamically creating the other pages in the background.
I’ve tried it with a background worker on a test form and it works (i am a bit surprised lol) Can/Should controls be created on non-ui-threads? i’ve added a button to my test form to modify label color and text of the label created in the background worker and that works too…? Still surprised 😅
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
Thread.Sleep(5000);
var label = new Label();
label.BackColor = Color.Red;
label.Text = "test";
label.TextChanged += (_, _) => MessageBox.Show("Whaaaat?");
backgroundWorker1.ReportProgress(0, label);
}
private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
Label label = (Label)e.UserState;
this.Controls.Add(label);
}
Is this a good approach to reduce loading/instantiation time of forms with many many controls?
3