BobSki
BobSki

Reputation: 1552

Child Forms and Parent forms mismatch in Vb.net

I have a parent form called MdiForm1 within it I open frm1 which is a child form. SO everything is great at this point - now I need to open another form within frm1 - lets call that frmX and here's where my issue arises - I previously had declared it as mdichild form, and did mdichild.show - however the issue comes up because when this form opens (it covers about 1/3 of frm1 - which is already open) and user clicks outside of the frmX - it simply disapears. So I tried to .showDialog() however am unable to do that because it's not Top level and is a mdiChild therefore won't let me .showdialog(). here's my code...

    Private Sub cmd1_Click(sender As Object, e As EventArgs) Handles cmd1.Click

    Dim NewMDIChild As New Frmx()

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

    'Display the new form
    NewMDIChild.ShowDialog()

    NewMDIChild.Top = 310
    NewMDIChild.Left = 36
    NewMDIChild.Width = 897
End Sub

I get this error on .showDialog() and here's what it says....

Form that is not a top-level form cannot be displayed as a modal dialog box. Remove the form from any parent form before calling showDialog.

I tried to declare the frmX like this....

dim frmX as New Form
frmX.showDialog

'specifying Top/Left/Width but that doesn't do anything, basically opens an empty form elsewhere on the screen.

EDIT:It's a little confusing :/

This is what I Did - getting the same error. This is in frm1 on button click which is suppose to OPEN frmX in modal so that users clicking on frm1 will not make frmX disappear. It opens in the right location however when I click elsewher on frm1 --- frmX disappears

    Dim frmxName As New FrmX()
    frmxName.MdiParent = Me.MdiParent
    frmxName.ShowDialog()
    frmxName.Top = 310
    frmxName.Left = 36
    frmxName.Width = 897

My goal is to have frmX open until they click close on it!

Upvotes: 0

Views: 1872

Answers (1)

djv
djv

Reputation: 15774

Set the new form's MDI parent to the controlling form's MDI parent

In the MDI parent I called Form1. This form has the property IsMdiContainer = True

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim myFrmX As New frmX
        myFrmX.MdiParent = Me
        myFrmX.Show()
    End Sub

End Class

And in frmX with a button on it

Public Class frmX 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim f As New Form
        f.MdiParent = Me.MdiParent
        f.Text = "frmY"
        f.Show()
    End Sub

End Class

Clicking the button creates new Forms, which are shown to be MDI children of the main form below

enter image description here

Or if you just want a dialog window, forgo the MDI business, and just show dialog

Public Class frmX 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim f As New Form
        f.Text = "frmY"
        f.ShowDialog()
    End Sub

End Class

Now frmY has focus until it's closed.

enter image description here

Upvotes: 1

Related Questions