MDI child forms' close causes errors  
Author Message
MiguelFigueiredo





PostPosted: Top

Visual Basic [VB] >> MDI child forms' close causes errors

I have an MDI parent form frmMain that *should* allow multiple instances
of a child form frmChild. The frmChild operates completely
independently of frmMain (aside from being an MDI child), and the
frmChild close event occurs via a button on frmChild (from within the
code of frmChild). I need to be able to dynamically create multiple
separate instances of frmChild from frmMain.

The problem:

If two (or more) frmChild's are opened, the close event for one affects
the class level variables and fires the event handlers of the combo
box's for all of the others.

Specifically:

frmChild has combo-boxes cboCountry and cboState. cboState is populated
depending upon the selection of cboCounty, from a data-set dsStates.
cboCountry.SelectedIndexChanged has an event handler
(cboCountry_SelectedIndexChanged) that builds cboState from the dsStates
dataset. On form close, dsStates is set to nothing (dsStates =
nothing).

This causes problems because:

The cboCountry_SelectedIndexChanged event handler fires (several times)
for every frmChild open and attempts to reference dsStates, which has
been set to nothing. (Object reference not set to an instance of the
object)

But:
(a) the cboCountry_SelectedIndexChanged should not fire on the the other
forms because they are (or should be) separate forms and
(b) the dsStates is (should be) a class level variable specific to the
one instance of the form it is referenced in (it should be a separate
referenced variable for each frmChild form).

'the <badbadbad> Code...
'********* in fmrMain *********

Private Sub mnuCreateChild_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles mnuCreateChild.Click

Dim objChild As New frmChild()

objChild.MdiParent = Me
objChild.Show()
Me.Refresh()

End Sub

'********* in frmChild *********

'class level variables

dim dsStates as new DataSet()

'form closing method

Private Sub frmChild_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
'some other stuff...
dsStates.Dispose()
dsStates = Nothing
End Sub

'event handler for cboCountry.SelectedIndexChanged

Private Sub cboCountry_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cboCountry.SelectedIndexChanged

If Loading = False Then

dsStates.Tables("States").DefaultView.RowFilter = "Country =
'" & cboCountry.SelectedValue.ToString & "'"

With cboState
.DataSource = dsStates.Tables("States")
.DisplayMember = "State"
.ValueMember = "State"
.SelectedIndex = 0
End With
End If
End Sub

*** Sent via Developersdex http://www.hide-link.com/ ***
Don't just participate in USENET...get rewarded for it!

Visual Studio230