One form "show" another form 
Author Message
 One form "show" another form

I am having trouble making one form "show" another form.   frmMain has a picturebox that when clicked should load frm2.

Public Class frmMain
    Inherits System.Windows.Forms.Form
    Dim frm2 As New frm2()

    Private Sub pic1_Click(ByVal _
        sender As System.Object, ByVal e As _
        System.EventArgs) Handles pic1.Click

        frmPortfolio.Show()
    End Sub
End Class

This works great for the first time the user clicks on the picturebox.  It shows the second form just like it should, and it prevents the user from firing off multiple copies of the secondary form.  But if the user closes the secondary form, and then clicks the picturebox again on the main form, it will not load the secondary form again.

The second time the user clicks the picture, after having closed frm2, it throws this error:

An unhandled exception of type 'System.ObjectDisposedException' occurred in system.windows.forms.dll

Additional information: Cannot access a disposed object named "frm2".

Any ideas?

Thanks,
Cliff Cavin
Columbus, Ohio
http://www.*-*-*.com/



Wed, 11 May 2005 22:06:36 GMT  
 One form "show" another form

  I am having trouble making one form "show" another form.   frmMain has a picturebox that when clicked should load frm2.

  Public Class frmMain
      Inherits System.Windows.Forms.Form
      Dim frm2 As New frm2()

      Private Sub pic1_Click(ByVal _
          sender As System.Object, ByVal e As _
          System.EventArgs) Handles pic1.Click

          frm2.Show()
      End Sub
  End Class

  This works great for the first time the user clicks on the picturebox.  It shows the second form just like it should, and it prevents the user from firing off multiple copies of the secondary form.  But if the user closes the secondary form, and then clicks the picturebox again on the main form, it will not load the secondary form again.

  The second time the user clicks the picture, after having closed frm2, it throws this error:

  An unhandled exception of type 'System.ObjectDisposedException' occurred in system.windows.forms.dll

  Additional information: Cannot access a disposed object named "frm2".

  Any ideas?

  Thanks,
  Cliff Cavin
  Columbus, Ohio
  http://BubbaDogSoftware.dnsalias.net



Wed, 11 May 2005 22:10:56 GMT  
 One form "show" another form
Thanks John.

If if put "Dim frm2 As New frm2" in the click event of the picturebox, it
allows multiple instances of frm2 to be loaded and visible at the same time.

Is there an easy way to say something like:

If frm2 already loaded
    then set focus to it
else
   dim frm2 as new frm2()
   frm2.show()
endif

Thanks again,,,,Cliff


Quote:
> Cliff

> When you close frm2 it executes the dispose method for the
> form (sort of the equivalent of setting a variable to
> nothing in VB6).

> To solve the problem put the "Dim frm2 As New frm2"
> statement in the click event procedure. Should work fine.



Wed, 11 May 2005 22:51:49 GMT  
 One form "show" another form
Add a module and maintain a counter there. If the count is 0 then you know
you have no Form2's running around. Dispose, subtract from that variable.
From2's load add to it.

HTH


Quote:
> Thanks John.

> If if put "Dim frm2 As New frm2" in the click event of the picturebox, it
> allows multiple instances of frm2 to be loaded and visible at the same
time.

> Is there an easy way to say something like:

> If frm2 already loaded
>     then set focus to it
> else
>    dim frm2 as new frm2()
>    frm2.show()
> endif

> Thanks again,,,,Cliff



> > Cliff

> > When you close frm2 it executes the dispose method for the
> > form (sort of the equivalent of setting a variable to
> > nothing in VB6).

> > To solve the problem put the "Dim frm2 As New frm2"
> > statement in the click event procedure. Should work fine.



Wed, 11 May 2005 23:37:02 GMT  
 One form "show" another form
Thanks Richard.

I'm sure ther must be a better way to do this, but, the public variable in
Module1.vb does seem to do the trick.

So now I have, in Module1.vb:
Public FormCounterPortfolio As Int16

And in frmMain.vb I have:
Private Sub picPortfolio_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles picPortfolio.Click

    If FormCounterPortfolio = 0 Then

          FormCounterPortfolio += 1

          Dim frmPortfolio As New frmPortfolio()

          frmPortfolio.Show()

    End If

End Sub

And in frmPortfolio.vb I have:
Private Sub frmPortfolio_Closed(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Closed

      FormCounterPortfolio -= 1

End Sub

This approach is a bit lumpy, but it gets the job done.

Thank you,,,,,,Cliff



Thu, 12 May 2005 00:22:05 GMT  
 One form "show" another form
I presume "Dim frm2 As New frm2()" should say "Dim frmPortfolio As New
frm2()"?

You only create the instance of frm2 when frmMain is instantiated. If you
then close frm2, the variable frmPortfolio is no longer valid, and is
pointing to a closed (or discarded) form.
You could use something like

    Private Sub pic1_Click(ByVal _
        sender As System.Object, ByVal e As _
        System.EventArgs) Handles pic1.Click

        If frmPortfolio.IsDisposed Then _
            frmPortfolio = New Form2()
        frmPortfolio.Show()
    End Sub

Cheers,
  Jason

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


I am having trouble making one form "show" another form.   frmMain has a
picturebox that when clicked should load frm2.

Public Class frmMain
    Inherits System.Windows.Forms.Form
    Dim frm2 As New frm2()

    Private Sub pic1_Click(ByVal _
        sender As System.Object, ByVal e As _
        System.EventArgs) Handles pic1.Click

        frmPortfolio.Show()
    End Sub
End Class

This works great for the first time the user clicks on the picturebox.  It
shows the second form just like it should, and it prevents the user from
firing off multiple copies of the secondary form.  But if the user closes
the secondary form, and then clicks the picturebox again on the main form,
it will not load the secondary form again.

The second time the user clicks the picture, after having closed frm2, it
throws this error:

An unhandled exception of type 'System.ObjectDisposedException' occurred in
system.windows.forms.dll

Additional information: Cannot access a disposed object named "frm2".

Any ideas?

Thanks,
Cliff Cavin
Columbus, Ohio
http://BubbaDogSoftware.dnsalias.net



Thu, 12 May 2005 07:18:17 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. How to use "unload form", "form.hide", "form.visible =false "?

2. How to use "unload form", "form.hide", "form.visible =false "?

3. How to use "unload form", "form.hide", "form.visible =false "?

4. How to "show" a Form

5. Forms "talking" to other forms

6. OO, "forms within forms"?

7. Form larger than "Form"

8. Form larger than "Form"

9. showing a from dynamically - "frmSample".show ?

10. If form is "hide" or "show"

11. Problem understanding "Current Record" on form

12. Eliminate Form "Flickering"

 

 
Powered by phpBB® Forum Software