• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

Problem drawing controls on screen

Hello experts,

I have a huge problem with my planning application. I have create an application that shows the planning for an employee. To do so, I created an object 'Planningline', with a panel on it. In this panel, I add colored blocks to show the planning with the function myPanel.Controls.Add( myBlock). For each employee, I add such a planningline in another panel, and this is shown on the screen.

This works, but it seems that after a while the system runs out of object or memory. I show about 100 lines, and they have a function to 'refresh' the form, which re-creates all objects. Before I do this, I added a this code to clear the memory:

dim myControl as control
for each control in myPanel.Controls    
     myControl.Dispose()
     myControl = Nothing
Next
myPanel.Controls.Clear()
System.GC.Collect

This is done for all panels: there is a loop for the 'planningline' objects, and for every planningline object this is done again for the panel on this object.

However, this doesn't seem to work, after a few 'refresh' actions the system stops drawing objects, and I cannot even access the menu anymore. The only solution is to restart the program.
I also use a datasource to find the employees, but this datasource is defined as a local variable. Could this be the problem ?
0
ACAE
Asked:
ACAE
1 Solution
 
SanclerCommented:
I don't think your code is doing what you think it is.

dim myControl as control
for each control in myPanel.Controls    
     myControl.Dispose()
     myControl = Nothing
Next
myPanel.Controls.Clear()
System.GC.Collect

The first line is declaring myControl as an instance of the object Control.  Then, within your loop, you are constantly disposing of THAT instance.  Your code is not disposing of the controls in the myPanel.Controls collection, even though you are using the enumerator of that to determine how many times to dispose of that instance.  You need something like

   While myPanel.Controls.Count > 0
       Dim myControl As Control = myPanel.Controls(0)
       myPanel.Controls.Remove(myControl)
       myControl.Dispose()
       myControl = Nothing
   End While

The other stuff - about .Clear and .Collect - should not then be necessary.

Roger
0
 
ACAEAuthor Commented:
Roger,

Thank you very much, this works fine now !!
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now