public class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainWindow());
}
}
class MainWindow : Form
{
string[] list = new string[1548];
TableLayoutPanel panel = new TableLayoutPanel();
public MainWindow() : base()
{
Height = 2000;
Width = 1000;
Random rand = new Random();
for (int i = 0; i < 1548; i++)
{
list[i] = rand.Next().ToString();
}
Button button = new Button();
button.Text = "Press me";
button.Click += Button_Click;
panel.Controls.Add(button, 0, 0);
panel.Height = 2000;
panel.Width = 1000;
Controls.Add(panel);
Show();
}
private void Button_Click(object sender, EventArgs e)
{
for (int i = 0; i < 36; i++)
{
ComboBox box = new ComboBox();
box.DataSource = list;
panel.Controls.Add(box, 0, i+1);
}
}
}
This up here will create a window with a button and will pre-generate a list of 1548 random integers.
When you press the button 36 ComboBoxes will be created and populated with the pre-generated list. It will hang for a second or two.
Change it with box.Items.AddRange(list); and it will be even slower.
If you now comment line box.DataSource = list;, it will be significantly faster.
I don't understand why does it take so long to bind a data source to a comboBox. I'm guessing it has something to do with drawing the drop down menu.
Anyway, the comboBoxes I use in my program are all the same. They show the same list, but are binded to different "data slots" (they enable you to select an inventory item - list of items is same for every slot, but what is selected isn't).
Is there any way I can somehow force the program to draw only one ComboBox and use it for all 36? Od do something similar. Seems like a waste of resources to have it do same thing for 36 times...
It would speed up the startup time of the program.