I have an Mdiform which with help of menustrip opens Form1.
From this form(Form1) I open another form i.e Form2.which I want also to be a part of my mdiform
I created property of mdiform and mapped it in my form2 when it loads.but still it does not become a part of mdiform.
Another problem is ,it keeps generating new instances of form2 whenever I again click button in form1,which is child of my mdiform.I want that only single instance of form2 should be visible.
I tried doing different ways but still generates new instances.
Can anyone explain this?
Thanks in advance

5 Years
Discussion Span
Last Post by Pgmer

See if this helps to load Form2 from Form1, into the MDI Form.

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        With Form2
            .MdiParent = myCoolMDIform '// set the MDI Parent for the Form.
            .Show() '// Load the Form.
        End With
    End Sub
End Class

AndAlso, this to Not create a new.instance of Form2.

Public Class Form2

    Private Sub Form2_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True '// .Cancel closing the Form.
        Me.Hide() '// ...and .Hide it.:)
    End Sub
End Class

Sorry this does not give soliution to my problem.
I have a form which is a mdiparent form Nmaed as ParentMDIForm.The menustrip in it
opens another form that is studentform which child of parentmdiform.
i hav set it as this.mdiparent=this that works fine.but A button in studentform opens
Form3 which is giving problem.
On button_clcik in studform
code is
Form3 f3=new Form3();
f3.MdiParent=ParentMdiForm ----this is a from name of mdi form ,gives error
I tried by creating property of parent form in studform
then set it as
f3.MdiParent=parentfrm---this also gives object reference not set as error

not getting it


>>Form3 f3=new Form3();
That looks entirely like C#.code and will "ALWAYS" give you an error in VB.NET.

AndAlso, as soon as you declare as "New Form3", you will "ALWAYS" load a new.instance of a Form.


So you need to check if already an instance of form is opened or not.. u can use singleton pattern.


Try something like this..

Private Shared _Instance As Form2 = Nothing
Public Shared Function Instance() As Boolean
        If _Instance Is Nothing OrElse _Instance.IsDisposed = True Then
            _Instance = New Form2
            Return False
        End If
        Return True
    End Function

In calling form write this

If Form2.Instance = False Then
        End If

Code has solved my problem for creating single instance of open form but the form2 still
does not become a part of an mdiparent form.I tried in different ways as I mentioned with code earlier in my post, could you pls check that?


before calling u need to set

If Form2.Instance = False Then
' here u need to set its parent form.
        End If
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.