Closing forms...

The problem is that i have a global form object and it is opened from other forms. If the open form is closed in any of these forms, the global oject is disposed and can't be used any more without the whole application being re-executed.

Form is defined as:
Public WithEvents frmAdd As Main

I don't know if hiding them is an option, because...

a) Form1's datagrid is refreshed from Form2
b) Form2 is passed parameters when opened from Form1

any ideas??

I keep getting errors now and then that the form is disposed.


thanks.


KS
LVL 11
Ramesh SrinivasTechnical ConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

planoczCommented:
Try this ....

Public frmAdd as new form

thats all you need.
Ramesh SrinivasTechnical ConsultantAuthor Commented:
I still get the same problem with that.
planoczCommented:
This will need to be in a Module from

Public frmAdd as new form

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Olaf_RabbachinCommented:
I suggest that, instead of using a simple global reference to the form, implement a public property. This way, you could have that property create an instance of the form and open it up if it's not (yet/anymore) there.

I.e. (air-code!):

public class MainFormAccess
   private shared mfrm as Main

   public readonly property MainForm as form
      Get
         if mfrm is nothing then
            'place the code required to open and i.e. initialize anything on it here
         end if
         Return mfrm
      End Get

   end property
end class
Ramesh SrinivasTechnical ConsultantAuthor Commented:
Would I have to put this class in all pages???
Olaf_RabbachinCommented:
The class is just a sample - put it anywhere you want. Note that the variable and the property are to be marked as "shared", meaning that you do not have to create an instance of the class, you just use the shared methods it offers.
Reading my code again - I forgot to include the "Shared"-keyword for the property, sorry. It should read:

public shared readonly property MainForm() as Form
...

Hence, just use the above class, with whatever additions you may require, as a stand-alone class, best in its own file. When refering to your main form, use the property contained instead of the form itself.
With the class named as in my sample and a public function "UpdateSomeStuff" within it, this could look like this for instance:

...
MainFormAccess.MainForm.UpdateSomeStuff

Hope I clarified things there ... :-)

Cheers,
Olaf
Ramesh SrinivasTechnical ConsultantAuthor Commented:
Okay, i need to know exaclt what to do...

Would I have to make to separate stand-alone classes for all the forms i am opening and closing?

I dont quite understand why I would need an update function in that class file also? Do you mean that for any updates to the form i.e. rebinding the grid, I would need to make a call to the class??

thanks,


KS
Olaf_RabbachinCommented:
I don't quite understand. Your original question targetted the situation where you are using some form which is being referenced globally and, because of the fact that it might be closed during runtime, that errors resulted from referencing the (what you called) "global object".
Hence, my suggestion was to simply build a "wrapper" around it so that just this wrapper would open up the form in case it had been closed.
So what you should be doing (if the above is correct) would be to:
- create a new class (such as the MainFormAccess-class I posted earlier)
- replace your "global object" with the property in that class that returns a reference to the form

That should be all!

Cheers,
Olaf
Ramesh SrinivasTechnical ConsultantAuthor Commented:
I have th following...

Public Shared ReadOnly Property MainForm() As Form
            Get
                If mfrm Is Nothing Then
                    'place the code required to open and i.e. initialize anything on it here
                    mfrm = New Main
                    mfrm.Show()
                End If
                Return mfrm
            End Get

        End Property

Is this what I'm meant to do??

"replace your "global object" with the property in that class that returns a reference to the form"


I dont know how to open the form with what you have supplied, as its a property??

E.g. this does not work: FormOpener.MainFormAccess.MainForm()


Can you just provide me with complete code, for opening the form using your class as that would be much clearer.

thank you,

KS



Ramesh SrinivasTechnical ConsultantAuthor Commented:
My global definition of the form to open is currently this:

Public WithEvents frm As Main
Ramesh SrinivasTechnical ConsultantAuthor Commented:
Keep my form delcarations in a module seems to work.

thanks all.
Olaf_RabbachinCommented:
Hi saleek,

alright, here's a sample I can come up with. It includes a simple form with but a single label. If you insert that form into a new project, set the startup-object to be <sub Main()>.
From there, I'm using the CFormWrapper-class instead of a direct call to the main form.

Does that clarify things?

Cheers & HTH,
Olaf

--- 8< ---

Public Class frm_Main
   Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

   Public Sub New()
      MyBase.New()

      'This call is required by the Windows Form Designer.
      InitializeComponent()

      'Add any initialization after the InitializeComponent() call

   End Sub

   'Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
      If disposing Then
         If Not (components Is Nothing) Then
            components.Dispose()
         End If
      End If
      MyBase.Dispose(disposing)
   End Sub

   'Required by the Windows Form Designer
   Private components As System.ComponentModel.IContainer

   'NOTE: The following procedure is required by the Windows Form Designer
   'It can be modified using the Windows Form Designer.  
   'Do not modify it using the code editor.
   Friend WithEvents lblSomeText As System.Windows.Forms.Label
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
      Me.lblSomeText = New System.Windows.Forms.Label
      Me.SuspendLayout()
      '
      'lblSomeText
      '
      Me.lblSomeText.Location = New System.Drawing.Point(16, 16)
      Me.lblSomeText.Name = "lblSomeText"
      Me.lblSomeText.Size = New System.Drawing.Size(248, 23)
      Me.lblSomeText.TabIndex = 0
      Me.lblSomeText.Text = "Just some demo-text"
      '
      'frm_Main
      '
      Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
      Me.ClientSize = New System.Drawing.Size(288, 54)
      Me.Controls.Add(Me.lblSomeText)
      Me.Name = "frm_Main"
      Me.Text = "Form1"
      Me.ResumeLayout(False)

   End Sub

#End Region


End Class

Public Class Main

   Public Shared Sub Main()
      MessageBox.Show(CFormWrapper.MainForm.lblSomeText.Text)
   End Sub

End Class

Public Class CFormWrapper
   Private Shared mfrm As frm_Main

   Public Shared ReadOnly Property MainForm() As frm_Main
      Get
         If mfrm Is Nothing Then
            mfrm = New frm_Main
            mfrm.Show()
         ElseIf Not mfrm Is Nothing AndAlso mfrm.Visible = False Then
            mfrm.Visible = True
         End If
         Return mfrm
      End Get
   End Property

End Class
planoczCommented:
Olaf_Rabbachin nice demo !!    :)
Olaf_RabbachinCommented:
planocz - thanks! But it still seems that saleek found your statement to be more valuable. :-)

Cheers,
Olaf
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.