Destructor or unload form?

I have an instance of a treeview, with 4 sets of nodes. They are nested and are (from parent to child...):
Company --> Department --> Location --> WorkEquipment

Selecting any one of these opens a form in an adjacent window (see code below). Right now, once the form is open it stays open and is just covered up by the next one. The app needs to close the open form when another node is selected. Do I need to write some sort of destructor, or use a Form.Unload type of command? Thanks.



Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
Dim NewMDIChild As New Form

If TreeView1.SelectedNode.Tag = "company" Then
NewMDIChild = New frmCompany

ElseIf TreeView1.SelectedNode.Tag = "department" Then
NewMDIChild = New frmDepartment
'Give the passed node
ElseIf TreeView1.SelectedNode.Tag = "location" Then
NewMDIChild = New frmLocation
ElseIf TreeView1.SelectedNode.Tag = "we" Then
NewMDIChild = New frmWorkEquipment
End If
gstrPassedNode = TreeView1.SelectedNode.Text

'Set the Parent Form of the Child window.
NewMDIChild.MdiParent = Me

'Display the new form.
End Sub


  • John

    Why not redraw the form with new data when the node changes. Write a Clear sub and load with new data. Just Set up like this

    Public F2 as New Form2 'Done in Delecerations

    in clear data on form1

    With F2

    'code work on form2

    End With

    This might be easier if all data is similar and you don't have to worry about destructing or unloading.

  • Thanks for the tip. It might work, but each of the forms is entirely different. So, I just initiate an instance of one of them when a corresponding node is clicked. Seems as though I could redraw it, as you suggest, and just have one instance of a "master" form that gets redrawn each time, as one of the 4 types. I'll give it some thought. Thanks again.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion