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 ?
ACAEAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ACAEAuthor Commented:
Roger,

Thank you very much, this works fine now !!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.