Results 1 to 11 of 11

Thread: Closing a form from other form

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2004
    Posts
    19

    Closing a form from other form

    I am opening a form from other form, both need to be displayed at once, which I have been able to do. But what I need to be able is to close both forms when using a push button from only one of them.

    I have tried the following code in the pushbutton:

    Dim frmMain as new frmMain

    frmMain.close
    me.close

    But this opens a new frmMain, I have also tried:

    Dim frmMain a frmMain

    frmMain.close
    me.close


    but this generates an error.




    Thanks

  2. #2
    Member
    Join Date
    Mar 2004
    Location
    Middle of nowhere
    Posts
    45
    Ummm... Not sure exactly, but have you tried the

    "application.exit" command? it works wonders...
    it will close all forms under that current application.
    Not sure if it will work for you, as im not sure exactly what your doing, but it sounds like your doing something very similiar to something i've done in the past. And i came here asking the exact same question, and ya,that was the fix i got
    hope it helps

    ---Flac
    Everything great once started with the words "That will never catch on, you shouldnt even bother" be great, go against the crowed, do something stupid, you might become famous.

  3. #3
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi Flac,

    Interesting signature!!

    "do something stupid, you might become famous."


    You mean like politicians???
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2004
    Posts
    19

    Not what I was looking for

    I do not want to close out the application.

    I have a splash form, from the splash form I open another, lets call is Main for here. Then from the Main form when you press the Help button another form will open. What I then need is the Main form and help form to both be displayed, when you close the help form I need both the help and main form to close leaving only the splash form displayed.

    Thanks

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    "frmMain.close
    me.close"


    That code SHOULD work. It works for me.

    There must be something else at fault.

    You do not make it clear where you have declared frmMain. Try declaring the instance of frmMain as Public in the Module.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2004
    Posts
    19

    Still having an issue

    Here are more details

    App starts and loads the frmSplash

    Within the frmSplash are Buttons to make a selection:

    Here is the code within the Splash form:

    Private Sub rbHTTP_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbHTTP.CheckedChanged

    Dim frmHTTP As New frmHTTP

    frmHTTP.Show()

    End Sub

    When you select the HTTP button the frmHTTP is displayed correctly.

    On the frmHTTP is a button labeled "Help", when you press that button the frmHelp is displayed:

    Here is the code:

    Private Sub btHelp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btHelp.Click

    Dim frmPasswordHelp = New frmPasswordHelp

    frmPasswordHelp.show()

    End Sub

    At this point all three forms are displayed, and what I want to do is to press a button on the frmHelp that will close the frmHelp and the frmHTTP while leaving the frmSplash displayed:

    Here are some examples of the code in the frmHelp that has not worked:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    frmHTTP.Close()
    Me.Close()

    End Sub


    Also tried:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim frmHTTP As New frmHTTP

    frmHTTP.Close()
    Me.Close()

    End Sub

    All the above code does is to open a new frmHTTP and close it, leaving the orginal frmHTTP displayed.

    I also tried:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim frmHTTP As frmHTTP

    frmHTTP.Close()
    Me.Close()

    End Sub

    Still no luck, just got the following error:

    An unhandled exception of type 'System.NullReferenceException' occurred in WS2000 Simulator.exe




    Why did they mess up a good thing, it was so easy in VB6!!

  7. #7
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160
    VB Code:
    1. Dim frmHTTP As New frmHTTP
    2.  
    3. frmHTTP.Close()
    4. Me.Close()
    5.  
    6. End Sub

    You are creating a new Instance of FrmHTTP hence "As New"

    You need to create a Reference from the forms, using Public Properties is the easiest way


    VB Code:
    1. Public Class Form2
    2.     Inherits System.Windows.Forms.Form
    3.  
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.  
    6.         Dim frm As New Form3
    7.         frm.frm2 = Me
    8.         frm.Show()
    9.  
    10.     End Sub
    11. End Class
    12.  
    13.  
    14. Public Class Form3
    15.     Inherits System.Windows.Forms.Form
    16.  
    17.     Public frm2 As Form2
    18.  
    19.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    20.         frm2.Close()
    21.         Me.Close()
    22.     End Sub
    23. End Class
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    ref my previous post.

    If you are starting the application from frmSplash and not from a module, declare

    Public frmHTTP As New frmHTTP

    in the general section of frmSplash.

    then

    VB Code:
    1. frmMain.close
    2. me.close

    WILL work.

    You are declaring it as private but expecting to reference it from frm Help.


    Hi Dmyze,

    "You are creating a new Instance of FrmHTTP hence "As New""

    He has to create the instance. He has simply created it in the wrong place and with the wrong declaration. - unless I have completely misunderstood him.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2004
    Posts
    19

    Solved, I cheated :)

    I knew how to do it in VB6, so I wrote a quick app in VB6 and ran the upgrade Wizard to VB.Net to see the code:


    Here is what I should have done...

    Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click

    Form3.DefInstance.Close()
    Form2.DefInstance.Close()


    End Sub


    Thanks for all your help

  10. #10
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    "Form3.DefInstance.Close()
    Form2.DefInstance.Close()"


    I cannot follow the above.

    If "Form3" is the base class and "DefInstance" is the name of the instance created, the above will not code.

    or

    "DefInstance" is not a property of a form.

    I am using VB.NET 2003. Are you certain you are using VB.NET?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Mar 2004
    Posts
    19

    IT WORKS!!!!

    Finally figured it out:

    Just a recap or what I am trying to do.

    I have three forms, from1, form2 and form3.

    When the programs starts form1 is shown, with a pushbutton. When you press the push button on form1, form2 is displayed. Form2 also has a push button which when pressed will display form3. So all three forms are displayed at once. Then when you press a button on form3, form2 and form3 need to close while form1 stays open.

    Here is the code....

    For Form1:


    Private Sub Button1_Click

    Dim Form2 As New Form2

    Form2.Show()

    End Sub


    For Form2:


    Private Sub Button1_Click

    Dim Form3 As New Form3

    If Form3.ShowDialog = DialogResult.OK Then
    Me.Close()
    End If

    End Sub


    For Form3:


    Private Sub Button1_Click

    DialogResult = DialogResult.OK
    me.close()

    End Sub


    Hope this helps others who may need to control several forms from one form. Thanks for everyones help and support.

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