Dadyar Salar

Dadyar Salar

  • NA
  • 64
  • 9.8k

c# dispose user control on the panel

Jun 23 2020 1:45 PM
i have many user control when i click on my menus button it shows that user control i want but i was looking on GDI objects on the task manager i found that when i go to another user control the GDI of the previous user control not relaesed and not dispose it is my code for adding user control to panel and dispose any user control on that panel before adding new user control:
 
  1. void showControl(Control control)  
  2.         {  
  3.             for (int ix = tlp4.Controls.Count - 1; ix >= 0; --ix)  
  4.             {  
  5.                 var ctl = tlp4.Controls[ix];  
  6.   
  7.                 ctl.Dispose();  
  8.             }  
  9.   
  10.             tlp4.BringToFront();  
  11.               
  12.             control.Dock = DockStyle.Fill;  
  13.             tlp4.Controls.Add(control);  
  14.             control.Focus();  
  15.             tableLayoutPanel2.Visible = false;  
  16.               
  17.             tlp4.Visible = true;  
  18.         }