Author Topic: Recommended method for switching between winforms (Visual Basic)  (Read 2074 times)

astroman

  • Newbie
  • *
  • Posts: 21
    • View Profile
Hello. I've completed several projects using AdvancedHMI and they've worked wonderfully. Unfortunately, today there was trouble at our plant and I had to switch back to RSView. Winforms simply would not open and my components would no longer update (until I restarted the PC). I think it may have something to do with the way through which I'm closing and opening winforms. Currently, I do this using show/hide (e.g. 
       
Code: [Select]
Dim SecondForm As New Grizzly
        SecondForm.Show()
        Me.Hide()

The program which I created had been running for ~5 days and likely ran out of memory. I've attached an Event Viewer log with the likely errors. My question is, is there a better method of switching winforms? Closing the form causes issues with the driver code (I'm using the PLCSLCMicroCom). Thank you very much for any suggestions.


Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5365
    • View Profile
    • AdvancedHMI
Re: Recommended method for switching between winforms (Visual Basic)
« Reply #1 on: January 13, 2015, 05:13:45 PM »
You want to make sure you are not creating a new instance of the form each time it is shown, otherwise you will run out of memory.

The FormChangeButton switches forms by using the built-in mechanism for creating a new form. This is done by simply referring to it by the name (e.g. Grizzly.show)

To do it in code, you would need somewhere to keep a global variable that all of the forms can access to hide and show the one instance of the form. Before showing, you can check to see if it has already been instanciated:

If MyForm isnot nothing then
   Myform=new Grizzly
end if
Myform.Show

astroman

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Recommended method for switching between winforms (Visual Basic)
« Reply #2 on: January 13, 2015, 05:33:21 PM »
I must be blind, I had never noticed the FormChangeButton. Now, memory usage doesn't increase after every form change and the transition itself is very quick. Thank you very much Archie!