Odin Sonnah
Odin Sonnah

Reputation: 11

Wait after showing MDI child form - alternative to DoEvents

A co-worker made some improvements to my app, but at the same time, he changed the whole app to use MDI child forms. So where I used to be able to use ShowDialog() to make the subroutine wait until the new form closed to continue, now I can't. I adapted some old VB6 code to try and replicate the same functionality, but it uses DoEvents() and I've been told to avoid that at all costs.

At this point I can't help but feel like there's just a fundamental flaw in my methodology here, and that there is probably a far simpler way to accomplish the same task.

    Private Sub Label_EditNode_Click(sender As Object, e As EventArgs) Handles Label_EditNode.Click
        Dim Frm As New frmVariableEditor With {
            .Tag = "FrmVariableEditor",
            .MdiParent = FrmMDIMain,
            .Dock = DockStyle.Fill,
            .Location = Me.Location,
            .Size = Me.Size,
            .myFieldName = "NodeClick",
            .myResultText = txbNodeClick.Text
        }

        Frm.Show()
        Call WaitOnFormUnload()

        txbNodeClick.Text = Frm.myResultText
    End Sub

    Public Sub WaitOnFormUnload()
        Dim bIsLoaded As Boolean = True
        Dim FormName As String = Me.MdiParent.ActiveMdiChild.Name ' Form.ActiveForm.Name
        Do While bIsLoaded
            bIsLoaded = False

            For Each f As Form In Application.OpenForms
                If f.Name = FormName Then
                    bIsLoaded = True
                    Application.DoEvents()
                    Exit For ' breaks the for loop
                End If
            Next f
        Loop
    End Sub

Upvotes: 0

Views: 195

Answers (1)

Caius Jard
Caius Jard

Reputation: 74605

Goes a something like this:

Private Sub Label_EditNode_Click(sender As Object, e As EventArgs) Handles Label_EditNode.Click
    Dim Frm As New frmVariableEditor With {
        .Tag = "FrmVariableEditor",
        .MdiParent = FrmMDIMain,
        .Dock = DockStyle.Fill,
        .Location = Me.Location,
        .Size = Me.Size,
        .myFieldName = "NodeClick",
        .myResultText = txbNodeClick.Text
    }

    AddHandler Frm.FormClosing, AddressOf RetrieveBlahData

    Frm.Show()

End Sub

Private Sub RetrieveBlahData(sender as Object, e as FormClosingEventArgs)
   txbNodeClick.Text = DirectCast(sender, frmVariableEditor).myResultText
End Sub

Probably doesn't get more minimal than that, except for maybe sharing a single data object:

    Dim Frm As New frmVariableEditor With {
        .Tag = "FrmVariableEditor",
        .MdiParent = FrmMDIMain,
        .Dock = DockStyle.Fill,
        .Location = Me.Location,
        .Size = Me.Size,
        .myFieldName = "NodeClick",
        .DataSetToBindTo = Me.DataSetWhatever
    }

You'll have to re-code your form so that it takes this passed in dataset (or other data container) and gets its data from it (databinding) .. then as the editor form is changing the data, it's actually changing the original dataset in the main form too, because they're the same thing

Upvotes: 0

Related Questions