Question on closing forms
Hey. Ok so i'm having a problem in my program, so instead of trying to find the bug right away, I decided to test the problem that I'm having by creating a new basic program with two forms. It seems that if I make a new instance of a form, and don't set it to visible, then that form won't run the .closing method. Here's what I did:
Form1- contains 1 button
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If f2 Is Nothing Then
f2 = New Form2
End If
End Sub
form 2
- contains 1 timer
- modified .closing method
VB Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
MsgBox("time to run the closing code")
Me.Close()
End Sub
VB Code:
Private Sub Form2_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
MsgBox("doing the actual closing now!")
Me.Dispose()
f2 = Nothing
End Sub
Module
VB Code:
Friend Module DuckyModule
Friend f1 As Form1 ' can now be accessed from anywhere.
Friend f2 As Form2
End Module
Ok so, the timer is designed to within 5 seconds close form2 when it starts. If I don't do anything about seting the form visible (like .showDialog) then when the timer calls .close() then nothing happens! If I put in .setDialog() then the when the timer calls .close() then it works!. Is this normal? Any ideas on what I could do?
Thanks
John
Re: Question on closing forms
Well I don't understand. The code you have set doesn't have the Form2 open with either showdialog or show. Here's the code that works for me that should work for you:
Form1:
VB Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If f2 Is Nothing Then
f2 = New Form2
f2.ShowDialog()
End If
End Sub
End Class
Friend Module DuckyModule
Friend f1 As Form1 ' can now be accessed from anywhere.
Friend f2 As Form2
End Module
Form2:
VB Code:
Public Class Form2
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
MsgBox("time to run the closing code")
Me.Close()
End Sub
Private Sub Form2_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
MsgBox("doing the actual closing now!")
Me.Dispose()
f2 = Nothing
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
End Class
This code worked for me.
Re: Question on closing forms
well the wya i have it designed is that the form2 isn't actually supposed to be visible. By making a new one of it, it actually starts the timer and stuff, but doesn't show it. But when I do this, it doesn't run the .closing() method. I have the make it visible inorder to use the .close method
Re: Question on closing forms
Then make it visible. You shouldn't make a new form2. Why is form2 being called at the beginning when the application starts? In my programs form2 isn't the startup form and it doesn't show up unless it is called.