Hello,

Could someone help me with this one?

I have developed a small application. It has a MDIParent form on which the other childforms are displayed. I have a Panel on the MDIParent on which there are buttons to call up the MDIChild forms. Now my issue is, when anyone of the buttons on the MDIParents form Panel are clicked, it should check if another Childform is display, if it is then it (the active childform is closed) and the new form will be displayed. This part is working fine from the code below. However, when there are no active MDIChild form on the MDIParent form and if the button is clicked, the 'Else' part of my code below is executed. This generates the error as 'Object reference not set to an instance of an object'. So what is wrong with my code below? Please help me get this one done. :confused:

Thanking you in advance..

Private Sub btnExecutive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExecutive.Click
       
        Dim MDIChildForm As New frmExecutiveInfo
        MDIChildForm.MdiParent = Me
        Try
            If MDIChildForm.MdiParent.ActiveMdiChild.Visible = True Then
                MDIChildForm.MdiParent.ActiveMdiChild.Close()
                MDIChildForm.Show()
            Else
                MDIChildForm.Show()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        
    End Sub
Member Avatar for Unhnd_Exception

The else part is not getting reached when there is no mdi child. Instead your starting the if off with .ActiveMdiChild.visible = true.

If there is no .ActiveMdiChild then your checking the visiblity of a form that doesn't exist. That were the object reference not set to an instance is coming from.

Use this instead:

If Me.ActiveMdiChild Is Nothing Then
    'show your new form
 Else
    Me.ActiveMdiChild.Close()
    'show your new form
 End If

Thanks Unhnd_Exception for the help.

It's working fine after your help.

Thanks once again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.