Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

vb.net how to open mdichild in mdiparent? using ToolStrip

This is my code, why is it when i open my first form (MdiParent) and when i open the frmCashin form (mdiChild) the child form still exceeds the parents form

Private Sub ToolStrip_Click(sender As Object, e As EventArgs) Handles ToolStrip.Click
    Dim formcashin As New frmPOSCashIn

    Dim formcashout As New frmPOSTCashout

    If CashIn.Selected Then
        formcashin.MdiParent = Me.MdiParent
        formcashin.Show()

    ElseIf Cashout.Selected Then
        formcashout.MdiParent = Me.MdiParent
        formcashout.Show()
    End If
End Sub

just like this in the picture (i use dual monitor), I am new in .net technology, I can drag the child form to outside the parent form

enter image description here

UPDATE

    If CashIn.Selected Then
        Dim formcashin As New frmPOSCashIn
        Dim formcashout As New frmPOSTCashout
        Dim mainform As New frmMain

        formcashin.MdiParent = mainform.MdiParent
        formcashin.Show()

    ElseIf Cashout.Selected Then
        Dim formcashin As New frmPOSCashIn
        Dim formcashout As New frmPOSTCashout
        Dim mainform As New frmMain

        formcashout.MdiParent = mainform.MdiParent
        formcashout.Show()
    End If
question from:https://stackoverflow.com/questions/65914487/vb-net-how-to-open-mdichild-in-mdiparent-using-toolstrip

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Based on the code you posted, I assume that frmMain represents your main form (which is the MDI parent form) and that frmPOSCashIn and frmPOSTCashOut are two MDI child forms. And based on the screenshot, the toolstrip seems to be included in frmMain.

To wrap up the comments by jmcilhinney and Jimi:

  • You should not explicitly create a mainform variable. You can just use Me, since you are already executing within a frmMain form instance.
  • Since your frmMain instance itself is the MDI container (its IsMdiContainer property is set to True), it itself should be used as the value for the MdiParent property of your child forms (whose IsMdiContainer properties are set to False). (The MDI architecture allows only one level of hierarchy. You cannot nest MDI container forms as MDI children in an(other) MDI container form. So you should never use the MdiParent property of an MDI container form.)
  • You can declare your MDI child form variables inside the If-block. And in each block, you should only declare the variable that you actually need.

Something like this:

Private Sub ToolStrip_Click(sender As Object, e As EventArgs) Handles ToolStrip.Click
    If CashIn.Selected Then
        Dim formcashin As New frmPOSCashIn
        formcashin.MdiParent = Me
        formcashin.Show()
    ElseIf Cashout.Selected Then
        Dim formcashout As New frmPOSTCashout
        formcashout.MdiParent = Me
        formcashout.Show()
    End If
End Sub

If you are new to this stuff, I would suggest that you first check out some tutorials/walkthroughs in the Microsoft documentation or other sources on the Internet.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...