[RESOLVED] Setting object(Form) to nothing from within the object
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [RESOLVED] Setting object(Form) to nothing from within the object

  1. #1
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    [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..) ..

    Thanks

    Gremmy...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  2. #2
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    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.

    *Guarantee may not be honoured.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,099

    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 Handles Me.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
    And it gave me the correct results.

    I hope my post was useful boet!

  4. #4
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    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..

    Well the problem is solved Thanks Guys...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  5. #5
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    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.

    *Guarantee may not be honoured.

  6. #6
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Setting object(Form) to nothing from within the object

    Quote Originally Posted by javajawa View Post
    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...

    Gremmy...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center