Results 1 to 4 of 4

Thread: Question on closing forms

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    181

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         If f2 Is Nothing Then
    3.             f2 = New Form2
    4.         End If
    5.     End Sub

    form 2

    - contains 1 timer
    - modified .closing method

    VB Code:
    1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    2.         MsgBox("time to run the closing code")
    3.         Me.Close()
    4.     End Sub

    VB Code:
    1. Private Sub Form2_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
    2.         MsgBox("doing the actual closing now!")
    3.         Me.Dispose()
    4.         f2 = Nothing
    5.     End Sub


    Module

    VB Code:
    1. Friend Module DuckyModule
    2.     Friend f1 As Form1 ' can now be accessed from anywhere.
    3.     Friend f2 As Form2
    4.  
    5. 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

  2. #2
    Hyperactive Member sheikh78's Avatar
    Join Date
    Apr 2006
    Location
    C:/
    Posts
    423

    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:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         If f2 Is Nothing Then
    5.             f2 = New Form2
    6.             f2.ShowDialog()
    7.         End If
    8.     End Sub
    9. End Class
    10. Friend Module DuckyModule
    11.     Friend f1 As Form1 ' can now be accessed from anywhere.
    12.     Friend f2 As Form2
    13.  
    14. End Module

    Form2:
    VB Code:
    1. Public Class Form2
    2.  
    3.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    4.         Timer1.Stop()
    5.         MsgBox("time to run the closing code")
    6.         Me.Close()
    7.     End Sub
    8.  
    9.     Private Sub Form2_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
    10.         MsgBox("doing the actual closing now!")
    11.         Me.Dispose()
    12.         f2 = Nothing
    13.     End Sub
    14.  
    15.     Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    16.         Timer1.Start()
    17.     End Sub
    18. 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..."

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    181

    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

  4. #4
    Hyperactive Member sheikh78's Avatar
    Join Date
    Apr 2006
    Location
    C:/
    Posts
    423

    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
  •  



Click Here to Expand Forum to Full Width