[RESOLVED] Setting object(Form) to nothing from within the object
Well during testing the project came up with an interesting bug, If we close the form object (and dispose of it), it's object has not been set to Nothing, and now still responds to events..
Creating the Form ....
Code:
Private WithEvents FormVending As FrmVending
Private Sub MenuItemOnClick_Vend_CredRef(ByVal sender As Object, ByVal e As EventArgs)
FormVending = New FrmVending(thisUserID, thisUserName)
FormVending.MdiParent = Me
FormVending.Show()
End Sub
When the form is loaded it must get data from the comms object, everytime a token is inserted.. Like this..
Code:
Private Sub Comms_Mode_Token_In() Handles Comms_Mode.Token_In
ToolStripReaderStatus.Text = "[Token IN]"
If Not FormVending Is Nothing Then
FormVending.TokenData = Comms_Mode.TokenData
End If
End Sub
However when the form is closed, it is still replying to the above code.. even though i am disposing of it..
Code:
Private Sub FrmVending_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Me.DestroyHandle()
Me.Dispose(True)
End Sub
The thing is, I probably can simply add an event to the form so that the main class will set the object reference to Nothing. But i'm looking for a solution where i can set the object to nothing from with in the object, as i may have to add this to several other forms (many of which currently have not been setup for events..) ..
Re: Setting object(Form) to nothing from within the object
I'd personally solve the problem by doing it the other way round:
Code:
Private WithEvents mfDialog as frmDialog
Private Sub mfDialog_frmClosed(...) Handles ...
mfDialog = Nothing
End Sub
Then you can also use the frmClosing event to trap responses to data.
Doesn't really answer your question, but it's always seemed to work for me...
Help from me is always guaranteed!*
VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.
Re: Setting object(Form) to nothing from within the object
This looks like your "parent" form and FormVending's code are in one module because of WithEvents.
Look closely at your FrmVending_FormClosed event. You will notice, that it still HandlesMe.FormClosed
It should be :
Code:
Private Sub FrmVending_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles FormVending.FormClosed
FormVending.Dispose()
FormVending = Nothing
End Sub
I don't know if you have noticed, but if you had set a break point inside your FormClosed event, it would not have entered Break mode once you have closed FormVending, but only once you have closed the "parent" form.
I did a small test, that looked like this :
Code:
Public Class Form1
Private WithEvents FormVending As New Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
FormVending.Show()
End Sub
Private Sub FrmVending_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles FormVending.FormClosed
FormVending.Dispose()
FormVending = Nothing
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Not FormVending Is Nothing Then
MessageBox.Show("Not Nothing")
Else
MessageBox.Show("Nothing")
End If
End Sub
End Class
Re: Setting object(Form) to nothing from within the object
Thanks guy's you put me on the right path...
This code was in the form class (and not the main class), and thats why it handles Me.formclosed..
Code:
Private Sub FrmVending_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Me.DestroyHandle()
Me.Dispose(True)
End Sub
However i killed that sub and put it in the Main Form..
Code:
Private Sub FormVending_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles FormVending.FormClosed
FormVending.Dispose()
FormVending = Nothing
End Sub
Everything is working correctlly... Now i need to go through all 40 Forms and add the same in..
I was hoping there was something along the lines of Me = nothing, so that i could clear each form within it's own class, and not the owner class..
Re: Setting object(Form) to nothing from within the object
You might be able to generalise it using AddHandler, and testing the sender object against all of the forms you have defined...but that might get more tricky...
Help from me is always guaranteed!*
VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.
Re: Setting object(Form) to nothing from within the object
Originally Posted by javajawa
I'd personally solve the problem by doing it the other way round:
Then you can also use the frmClosing event to trap responses to data.
I normally use the Forms internal Formclosing event to check if the form may close or if an edit is still in progress..
Code:
Private Sub FrmSpecialTokens_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If _Close Then e.Cancel = True
End Sub
If and edit is in progress or any other reason to prevent the form from closing you set _Close to True ... To allow the form to close set it to False.. this even overides the "X" button on the form...
Bookmarks