Error on re-opening a form

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bole2cant

    Error on re-opening a form

    The error I get is:
    Cannot access a disposed Object named "Form2".
    Object name "Form2".

    Here is the sequence which gives the error.
    Start program. Form1 comes up and has a button to choose Form2.
    Go to Form2. Close Form2 when done. Now back on Form1.
    Click button to go back to Form2. The error/exception comes up.

    I originally got this error when using Me.Close(), so I changed it to Me.Hide() and everything was fine. Except now when the form is closed using the white X in the red button instead of the 'Close' button on Form2, I still get the error message. Apparently, clicking the white X is equivalent to Me.Close() and that won't let me re-open the form.

    What am I missing here?


    ****Code in Form1

    Public Class Form1

    Inherits System.Windows. Forms.Form

    Dim Form2 As New Form2

    Dim Form3 As New Form3



    ---------------------------------------

    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click

    Form2.Show()

    Form2.BringToFr ont()

    End Sub



    ****Code in Form2

    Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button2.Click

    Me.Hide()

    End Sub

    =============== ==============
    --
    -Doug

    Thanks Herfried for help with formatting decimals.
  • Ken Tucker [MVP]

    #2
    Re: Error on re-opening a form

    Hi,

    I would not name a form2 variable form2. Try this.

    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button1.Click

    If form2 is nothing then form2 = new form2

    Form2.Show()

    Form2.BringToFr ont()

    End Sub

    Ken
    ----------------

    "bole2cant" <sherwin@xxxmis sion.com> wrote in message
    news:40e5ba9c$0 $3135$99f61872@ news.xmission.c om:[color=blue]
    > The error I get is:
    > Cannot access a disposed Object named "Form2".
    > Object name "Form2".
    >
    > Here is the sequence which gives the error.
    > Start program. Form1 comes up and has a button to choose Form2.
    > Go to Form2. Close Form2 when done. Now back on Form1.
    > Click button to go back to Form2. The error/exception comes up.
    >
    > I originally got this error when using Me.Close(), so I changed it to
    > Me.Hide() and everything was fine. Except now when the form is closed using
    > the white X in the red button instead of the 'Close' button on Form2, I
    > still get the error message. Apparently, clicking the white X is equivalent
    > to Me.Close() and that won't let me re-open the form.
    >
    > What am I missing here?
    >
    >
    >
    > ****Code in Form1
    >
    > Public Class Form1
    >
    > Inherits System.Windows. Forms.Form
    >
    > Dim Form2 As New Form2
    >
    > Dim Form3 As New Form3
    >
    >
    > ---------------------------------------
    >
    > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
    > System.EventArg s) Handles Button1.Click
    >
    > Form2.Show()
    >
    > Form2.BringToFr ont()
    >
    > End Sub
    >
    >
    >
    > ****Code in Form2
    >
    > Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
    > System.EventArg s) Handles Button2.Click
    >
    > Me.Hide()
    >
    > End Sub
    >
    > =============== ==============[/color]

    --
    Outgoing mail is certified Virus Free.
    Checked by AVG Anti-Virus (http://www.grisoft.com).
    Version: 7.0.230 / Virus Database: 263.3.9 - Release Date: 7/2/2004


    Comment

    • Richard Myers

      #3
      Re: Error on re-opening a form

      > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As[color=blue]
      > System.EventArg s) Handles Button1.Click
      >
      > If form2 is nothing then form2 = new form2
      >
      > Form2.Show()
      >
      > Form2.BringToFr ont()
      >
      > End Sub
      >[/color]

      This wont always work. You really want

      If Form2 Is Nothing OrElse Form2.IsDispose d then Form2 = New Form2

      hth
      Richard


      Comment

      • bole2cant

        #4
        Re: Error on re-opening a form

        Thank you, Ken and Richard.

        Yes, Richard it did help! I can now go backward and forward to the forms.

        As a novice I don't really understand the code but for now that is okay.

        This program is my very first vb.net program and, while it is trivial, I have
        learned quite a bit. My only dissatisfaction with it is that after I fill in
        the three inputs (or change one) I can't just press 'enter' to activate the
        Calculate button.

        The .exe will soon be available at:


        If you wish to comment on it feel free--I've got thick skin--well, actually,
        since I got scalped for melanoma the skin is very thin, but that is another
        story. <g>

        -Doug

        =============== ========

        "Richard Myers" <richard.spam-free.myers@basd .co.nz> wrote in message
        news:eArekLIYEH A.2016@TK2MSFTN GP09.phx.gbl...[color=blue][color=green]
        > > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
        > > System.EventArg s) Handles Button1.Click
        > >
        > > If form2 is nothing then form2 = new form2
        > >
        > > Form2.Show()
        > >
        > > Form2.BringToFr ont()
        > >
        > > End Sub
        > >[/color]
        >
        > This wont always work. You really want
        >
        > If Form2 Is Nothing OrElse Form2.IsDispose d then Form2 = New Form2
        >
        > hth
        > Richard
        >
        >[/color]


        Comment

        • Richard Myers

          #5
          Re: Error on re-opening a form

          > Yes, Richard it did help! I can now go backward and forward to the forms.[color=blue]
          >
          > As a novice I don't really understand the code but for now that is okay.[/color]

          O.k its pretty straightforward . The Form2 Is Nothing part of the conditional
          is checking to see that the memory set aside for a Form2 object actually has
          something in it. The Form2.IsDispose d is checking to see whether the object
          found at the memory space has been disposed.

          Disposed is a state that an object enters into after it has been Disposed,
          that is had it's disposed method called on it. All forms have a disposed
          method, which is a special method that allows the form to clean up any
          resource handles it may have acquired during its lifetime. You'll note that
          we check whether the Form.IsDisposed after we checked whether the
          Form.IsNothing. This is because IsDisposed is a property of a form. If the
          form Is Nothing and we did the check the other way around i.e

          If Form2.IsDispose d OrElse Form2 Is Nothing then...

          we'd get a Null Reference exception because there is nothing there to check
          whether it has been disposed. Note the use of the OrElse conditional, called
          a "short circuit" conditional. If the first part of the conditional
          evaluates to
          true it doesn't evaluate the second half. Whereas if we had of just used
          "Or" the second half of the conditional would always be evaluated thus
          throwing a NullRef Exception when Form2 Is Nothing.


          When you open a form using .Show and then call .Close, the form instance is
          automatically disposed by the runtime, however when you open a form via
          ..ShowDialog it is not. Therefore you should always call
          MyForm.Dispose on forms opened via .ShowDialog.


          With respect to your other question about the enter key, there are a myraid
          of ways to accomplish this result. You could either handle the KeyPress
          event of the input fields or you could set the Calculate button as the
          AcceptButton of the form in question. Just check out the property table for
          Form2 and look for accept button.
          Note there is also a cancel button as well whose event is fired when a user
          hits the Escape key.

          Hth

          Richard



          [color=blue]
          >
          > This program is my very first vb.net program and, while it is trivial, I[/color]
          have[color=blue]
          > learned quite a bit. My only dissatisfaction with it is that after I fill[/color]
          in[color=blue]
          > the three inputs (or change one) I can't just press 'enter' to activate[/color]
          the[color=blue]
          > Calculate button.
          >
          > The .exe will soon be available at:
          > www.xmission.com/~sherwin/download2.html
          >
          > If you wish to comment on it feel free--I've got thick skin--well,[/color]
          actually,[color=blue]
          > since I got scalped for melanoma the skin is very thin, but that is[/color]
          another[color=blue]
          > story. <g>
          >
          > -Doug
          >
          > =============== ========
          >
          > "Richard Myers" <richard.spam-free.myers@basd .co.nz> wrote in message
          > news:eArekLIYEH A.2016@TK2MSFTN GP09.phx.gbl...[color=green][color=darkred]
          > > > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e[/color][/color][/color]
          As[color=blue][color=green][color=darkred]
          > > > System.EventArg s) Handles Button1.Click
          > > >
          > > > If form2 is nothing then form2 = new form2
          > > >
          > > > Form2.Show()
          > > >
          > > > Form2.BringToFr ont()
          > > >
          > > > End Sub
          > > >[/color]
          > >
          > > This wont always work. You really want
          > >
          > > If Form2 Is Nothing OrElse Form2.IsDispose d then Form2 = New Form2
          > >
          > > hth
          > > Richard
          > >
          > >[/color]
          >
          >[/color]



          Comment

          • Richard Myers

            #6
            Re: Error on re-opening a form

            Note i lose my way alittle below.
            All forms have a Dispose method, not a Disposed method.
            And IsDisposed is a property.

            Richard


            Comment

            • Programous

              #7
              RE: Error on re-opening a form

              You could also handle the Form2.Closeing. Set e.cancel = true, and then me.hide. That way you will not luse the data on the form.

              ~Programous

              "bole2cant" wrote:
              [color=blue]
              > The error I get is:
              > Cannot access a disposed Object named "Form2".
              > Object name "Form2".
              >
              > Here is the sequence which gives the error.
              > Start program. Form1 comes up and has a button to choose Form2.
              > Go to Form2. Close Form2 when done. Now back on Form1.
              > Click button to go back to Form2. The error/exception comes up.
              >
              > I originally got this error when using Me.Close(), so I changed it to Me.Hide() and everything was fine. Except now when the form is closed using the white X in the red button instead of the 'Close' button on Form2, I still get the error message. Apparently, clicking the white X is equivalent to Me.Close() and that won't let me re-open the form.
              >
              > What am I missing here?
              >
              >
              > ****Code in Form1
              >
              > Public Class Form1
              >
              > Inherits System.Windows. Forms.Form
              >
              > Dim Form2 As New Form2
              >
              > Dim Form3 As New Form3
              >
              >
              >
              > ---------------------------------------
              >
              > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
              >
              > Form2.Show()
              >
              > Form2.BringToFr ont()
              >
              > End Sub
              >
              >
              >
              > ****Code in Form2
              >
              > Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button2.Click
              >
              > Me.Hide()
              >
              > End Sub
              >
              > =============== ==============
              > --
              > -Doug
              >
              > Thanks Herfried for help with formatting decimals[/color]

              Comment

              Working...