Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Switching between forms in VB6 2

Status
Not open for further replies.

huskerdon

Programmer
Aug 26, 2002
67
US
Using VB 6 - I desperately need help with this problem of switching between forms in my app. I have 4 different displays, and they all have CPU-intensive graphics running, so it's not feasible to leave all 4 forms running, and just hide as necessary. I have tried everything I can think of. I realize that referencing the form or an object on it, will cause the form to be reloaded after it was already unloaded. I've taken every step to make sure that's not happening. This includes putting an 'Exit Sub' after the code that calls the unload event, so it doesn't continue and try to run more code. The switching of forms is triggered by conditions being met, and all 4 of these are tested during timer events.

What's happening, is that sometimes (NOT ALL), the new form will be loaded and displayed, but then a second later, the previous form will load again. Sometimes the unwanted form will be on top and sometimes it will be loaded, but behind the correct form, so you don't event know it's running without checking the taskbar. (All of these forms are maximized, so there's no borders or control boxes on them. The form can be minimized by clicking on a custom menu that I display on the form.)

What's very odd, is that I've run this app on 2 Win 98 machines for hours at a time, and this problem never occurs. I'm printing to the debug window, so I can go back and verify that only one form was running at a time. However on my new XP machine, which is 4x faster than the other 2, the problem occurs quite a bit.

It seems that if there was a problem in the code that was inadvertently causing the previous form to reload, it would happen all the time ?!?!?

Here's a very simplified example - currently form2 is running,

Private Sub tmr_pix_Timer()

If x >= 15 Then
Switch_Forms
Exit Sub
Else
' do normal routines - this code works fine
End If

End Sub


Private Sub Switch_Forms()

Select Case fnum ' public variable that determines which form to load next
Case 1
Form1.Show
Unload Me
Exit Sub
Case 2
' don't switch forms
' run code to reinitialize vars and stay on form 2
' this part works fine
Case 3
Form3.Show
Unload Me
Exit Sub
Case 4
Form4.Show
Unload Me
Exit Sub
Case 5
' error ...
End Select

End Sub

In the 'Form Unload' event for each of these, I'm doing the necessary cleanup, including writing to the registry if necessary. In one of the forms, in the 'Form Load', I have several things I'm doing and it takes a full 2 seconds to complete. Could this be interfering with the proper unloading of the previous form?
If anyone can suggest better ways of doing this, please let me know. From what I've read, I need the 'Exit Sub' after the 'Unload Me' to prevent the rest of the 'Switch_Forms' code from running. Should I 'show' the new form first, then unload the current form, or the other way around?
 
I would experiment with some code like

...
Case 1 ' For example
Unload Me
DoEvents
Form1.Show
...

This should force your UnLoad Event to run before you attempt to display the next form. I suspect that your fast XP machine is displaying Form1 and then the Unload Event on this form is being processed, forcing it to reacquire focus.

You shouldn't need the "Exit Sub". Once a Case condition has been satified and processed, control passes directly to the "End Select" it doesn't process the rest of the statement. That's why in

var = 1
Select Case var
Case 1
...
Case 1, 2
...
End Select

The code in "Case 1, 2" clause won't be executed.
 
Golom,

Thanks a lot for your suggestion. It seems so obvious now. Of course I didn't realize until a couple of hours ago that all code stays in memory even after a form is unloaded. I guess I was thinking that if you put 'Unload Me' first, then the subsequent code would be ignored. Now I know better! It's been running for an hour on the XP machine without any problem, and I'll test it on the Win 98 machines now. Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top