Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Closing a form

Closing a form

Scheduled Pinned Locked Moved Visual Basic
helpquestion
7 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jay Royall
    wrote on last edited by
    #1

    I have a windows application where a form opens another form, as showe din the code below: Public Class frmForm1 dim frmMyForm2 as frmForm2 public sub OpenForm2 If frmMyForm2 Is Nothing Then frmMyForm2 = New frmForm2() frmMyForm2.MdiParent = Me.ParentForm frmMyForm2.Show() Else frmMyForm2.BringToFront() End If end sub end class As you can see, if form2 is nothing then a new instance is created and displayed, else it is bought to the front. Problem arises when I close form2 and then try to re-open it from form1. Although the form is closed it still isn't equal to nothing, therefore, else clause is executed which obviously isn't any good. Any ideas?

    A 1 Reply Last reply
    0
    • J Jay Royall

      I have a windows application where a form opens another form, as showe din the code below: Public Class frmForm1 dim frmMyForm2 as frmForm2 public sub OpenForm2 If frmMyForm2 Is Nothing Then frmMyForm2 = New frmForm2() frmMyForm2.MdiParent = Me.ParentForm frmMyForm2.Show() Else frmMyForm2.BringToFront() End If end sub end class As you can see, if form2 is nothing then a new instance is created and displayed, else it is bought to the front. Problem arises when I close form2 and then try to re-open it from form1. Although the form is closed it still isn't equal to nothing, therefore, else clause is executed which obviously isn't any good. Any ideas?

      A Offline
      A Offline
      Ashfield
      wrote on last edited by
      #2

      Have you tried dispose in form2 when closing? frm2.close() frm2.dispose() That should get rid of it, or you could just hide it.# hope this helps

      Bob Ashfield Consultants Ltd

      J 1 Reply Last reply
      0
      • A Ashfield

        Have you tried dispose in form2 when closing? frm2.close() frm2.dispose() That should get rid of it, or you could just hide it.# hope this helps

        Bob Ashfield Consultants Ltd

        J Offline
        J Offline
        Jay Royall
        wrote on last edited by
        #3

        Yes, I have tried disposing, but for some reason that doesn't make form2 equal to nothing, so again, the else clause is being executed. Maybe I should just hide form2 instead of closing it then. Thanks for your help.

        L B 2 Replies Last reply
        0
        • J Jay Royall

          Yes, I have tried disposing, but for some reason that doesn't make form2 equal to nothing, so again, the else clause is being executed. Maybe I should just hide form2 instead of closing it then. Thanks for your help.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi,

          Liqz wrote:

          for some reason that doesn't make form2 equal to nothing

          whatever the class, there is not a single method that can set a reference to Nothing. Close() does not, Dispose() does not, Hide() does not. the only thing that makes form2 equal Nothing is a statement such as form2 = Nothing You have two ways to solve your problem: 1. never call close; just work with Hide() and Show() 2. make sure you apply form2 = Nothing whenever you close form2. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          Voting for dummies? No thanks. X|


          J 1 Reply Last reply
          0
          • J Jay Royall

            Yes, I have tried disposing, but for some reason that doesn't make form2 equal to nothing, so again, the else clause is being executed. Maybe I should just hide form2 instead of closing it then. Thanks for your help.

            B Offline
            B Offline
            BDEz Member 3919223
            wrote on last edited by
            #5

            Lets say that you have a form called Form1 and you want to check to see if it is open first so you dont have multiple instances of the form running around on your end user's desktop. First we make a Class statement and define your Form1 as FormNumberOne with events: Dim WithEvents FormNumberOne As Form = Form1 Then under an action like a menu item click or something we drop this if statement: If FormNumberOne Is Nothing OrElse FormNumberOne.IsDisposed Then Dim FormNumberOne As New Form FormNumberOne = Form1 FormNumberOne.Show() ElseIf FormNumberOne.Visible = False Then FormNumberOne.Show() Else MsgBox("You already have the form number one open") End If Give this a try, should be what you are looking for...

            1 Reply Last reply
            0
            • L Luc Pattyn

              Hi,

              Liqz wrote:

              for some reason that doesn't make form2 equal to nothing

              whatever the class, there is not a single method that can set a reference to Nothing. Close() does not, Dispose() does not, Hide() does not. the only thing that makes form2 equal Nothing is a statement such as form2 = Nothing You have two ways to solve your problem: 1. never call close; just work with Hide() and Show() 2. make sure you apply form2 = Nothing whenever you close form2. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Voting for dummies? No thanks. X|


              J Offline
              J Offline
              Jay Royall
              wrote on last edited by
              #6

              But I would have to execute 'form2 = nothing' from form1, how does form1 know when form2 is closing?

              L 1 Reply Last reply
              0
              • J Jay Royall

                But I would have to execute 'form2 = nothing' from form1, how does form1 know when form2 is closing?

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                If form1 is interested in what happens to form2, it could subscribe to its events, in your case the Closed event. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Voting for dummies? No thanks. X|


                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups