Ramesh Srinivas
asked on
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
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
ASKER
I still get the same problem with that.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
Would I have to put this class in all pages???
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.Up dateSomeSt uff
Hope I clarified things there ... :-)
Cheers,
Olaf
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.Up
Hope I clarified things there ... :-)
Cheers,
Olaf
ASKER
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
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
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
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
ASKER
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
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.
Can you just provide me with complete code, for opening the form using your class as that would be much clearer.
thank you,
KS
ASKER
My global definition of the form to open is currently this:
Public WithEvents frm As Main
Public WithEvents frm As Main
ASKER
Keep my form delcarations in a module seems to work.
thanks all.
thanks all.
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.ICon tainer
'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.Debugg erStepThro ugh()> 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.lblSome Text)
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(CFormWrapp er.MainFor m.lblSomeT ext.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
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.ICon
'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.Debugg
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.lblSome
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(CFormWrapp
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
Olaf_Rabbachin nice demo !! :)
planocz - thanks! But it still seems that saleek found your statement to be more valuable. :-)
Cheers,
Olaf
Cheers,
Olaf
Public frmAdd as new form
thats all you need.