I want to open an Instance of a form in MDI Container

here is my code
I just want an instance of a form to Edit records in the database

Private Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
        Dim Emp As New Employee()

        Emp.MdiParent = Me
        Emp.Show()


    End Sub

I'm getting this error when I click on Edit button

Form that was specified to be the MdiParent for this form is not an MdiContainer. Parameter name: value

I want the new form to open inside the MDI container

Dani AI

Generated

The exception means the value you assign to MdiParent is not the container form. In your first snippet, if the handler runs inside the Employee child, Me refers to that child, not the MDI parent. Even with IsMdiContainer = True (your screenshot), you will still get this error if you set MdiParent = Me. Point to the real container (your Mainfrm) or reuse the current child’s parent.

Example from inside an existing MDI child (per ’s follow-up), opening another child in the same container:

Private Sub BtnEdit_Click(sender As Object, e As EventArgs) Handles BtnEdit.Click
    Dim emp As New Employee()
    emp.MdiParent = Me.MdiParent   ' not Me
    emp.Show()
End Sub

To avoid spawning duplicates when the Edit button is clicked repeatedly (which you noted), search for an existing child and activate it; otherwise create one. Put this on the MDI parent:

Private Sub BtnEdit_Click(sender As Object, e As EventArgs) Handles BtnEdit.Click
    Dim existing As Form = Nothing
    For Each f As Form In Me.MdiChildren
        If TypeOf f Is Employee Then existing = f : Exit For
    Next
    If existing IsNot Nothing Then
        existing.WindowState = FormWindowState.Normal
        existing.Activate()
    Else
        Dim frm As New Employee()
        frm.MdiParent = Me
        frm.Show()
    End If
End Sub

For the new button on the instance form (your Post #8), add it to the Employee child and use it to toggle edit/save rather than spawning another window. If you are using data binding, a common pattern is:

Private Sub btnSave_Click(...) Handles btnSave.Click
    Me.Validate()
    EmployeesBindingSource.EndEdit()
    TableAdapterManager.UpdateAll(EmployeesDataSet)
End Sub

Side note: this is WinForms, not ASP.NET, despite the tag.

Recommended Answers

All 12 Replies

In the properties of the MdiParent look for IsMdiContainer and set that to true.

as you can see in the screen shot my ISMdiContainer is set true

MDI

Did you start the form as an MDI Parent Form? I'm not sure of everything that's required to make a form an MDI Parent but I suspect that there is an obscure property that is required. Using the standard template and modifying it might bring better results.

The screenshot I provided with the properties is the parent MDI form ,and inside Employee form I have a button EDIT(for editing recored)I have put the above code in that and I want the new window to open inside the parent MDI form

How can I do that?

assign mainform as the parent for the new form and use the show method

NewForm.MDIParent = MainForm
NewForm.Show()

Thank you "tinstaafl" one hurdal clear ,many more to go.:)

As I said now again a new hurdal >>>

Private Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
        Dim Emp As New Employee()
        Emp.MdiParent = Mainfrm
        Emp.Show()
        DGVload1()
        BtnEdit.Text = "new edit"
    End Sub

Now I want to make a new button on the instance form ,because whenever I click on the edit button (Old button)new form is opened.I want a button where I can edit content in the tables .

Are you talking about a button in the toolbar or on the form?

on the new form

You can easily add it in the design window, then double-click it and the code stub will show. Add your code in there. If you want it in a toolbar, just add one to the mdi child the same as you added for the mdi parent

Form that was specified to be the MdiParent for this form is not an MdiContainer.
Parameter name: value

i got this error how can i solve it please help me

Set the IsMdiContainer property to True on your parent form.

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.