|
-
Jul 9th, 2006, 12:51 PM
#1
Thread Starter
Addicted Member
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
-
Jul 9th, 2006, 01:49 PM
#2
Hyperactive Member
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.
"Imagination is more important than knowledge" - Albert Einstein, born on March 14th 1879.
Can't find it here on VBForums? Go to the CodeProject. MSDN is your friend . I have such a bad website, my friend decided it would be funny to change the template and he moderates the site for me: visit my site!
"Thinking of you, wherever you are
We pray for our sorrows to end, and hope that our hearts will blend.
Now I will step forward to realize this wish.
And who knows, starting a new journey may not be so hard…
Or maybe it has already begun.
There are many worlds, but they share the same sky
one sky, one destiny..."
-
Jul 9th, 2006, 02:23 PM
#3
Thread Starter
Addicted Member
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
-
Jul 9th, 2006, 04:21 PM
#4
Hyperactive Member
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.
"Imagination is more important than knowledge" - Albert Einstein, born on March 14th 1879.
Can't find it here on VBForums? Go to the CodeProject. MSDN is your friend . I have such a bad website, my friend decided it would be funny to change the template and he moderates the site for me: visit my site!
"Thinking of you, wherever you are
We pray for our sorrows to end, and hope that our hearts will blend.
Now I will step forward to realize this wish.
And who knows, starting a new journey may not be so hard…
Or maybe it has already begun.
There are many worlds, but they share the same sky
one sky, one destiny..."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|