Dear Experts,
I am using "using" statements to create labels dynamically.So the objects can be disposed after added to flow layout panel. Here is my code.
The problem is the control created are not added to the flow layout panel.
Code :
for (int j = 0;j<10 ; j++)
{
using (Label lbl = new Label())
{
lbl.Text = "Label" + j.ToString();
lbl.Name = "Label" + j.ToString();
lbl.Font = new Font("verdana", 6.0f);
lbl.Size = new Size(43, 8);
//lbl.Click +=new EventHandler(lbl_Click);
//flowLayoutPanel1.Margin = new Padding(0, 0, 0, 0);
//flowLayoutPanel1.Padding = new Padding(0, 0, 0, 0);
this.flowLayoutPanel1.Controls.Add(lbl);
}
}
{
using (Label lbl = new Label())
{
lbl.Text = "Label" + j.ToString();
lbl.Name = "Label" + j.ToString();
lbl.Font = new Font("verdana", 6.0f);
lbl.Size = new Size(43, 8);
//lbl.Click +=new EventHandler(lbl_Click);
//flowLayoutPanel1.Margin = new Padding(0, 0, 0, 0);
//flowLayoutPanel1.Padding = new Padding(0, 0, 0, 0);
this.flowLayoutPanel1.Controls.Add(lbl);
}
}
Any help appreciated.
Thanks, Shankar M

Eric ZimmermanPosted Jan 30, 2014, 2:00 AM
2. If you start to notice real performance issues, I would look into Data Virtualization... I know that the DataGrid has features like this for the same reason.
3. You don't really need to call dispose directly. It is a best practice to dispose large item or objects which wrap unmanaged resources (e.g. files, images, etc.)
Shankar MPosted Jan 30, 2014, 1:37 AM
Yes. But how this can done ? I will be loading nearly 1000-2000 labels dynamically and eventually it will increase the user objects if I don't dispose.
SUNIL GUTTAPosted Jan 30, 2014, 1:27 AM
what ever manner you write dispose function in for loop to dispose control that control is not showing on form ..
So just to get label the idea of doing disposing ..
Tested like :
The output will be 1-8 labels but lable9 wont be visible thats says disposing current senario is tough .. Interesting question hope VULPES show some different way .
Regards
Eric ZimmermanPosted Jan 30, 2014, 1:07 AM
I think you answered your own question.
You are disposing of the Label before it ever gets drawn by using the "using" statement.
Just remove the using statement.. The labels will get disposed normally when the flowpanel is disposed.
Regards,
Eric