is there a way to determine which MDIChild form is currently active, when all of them are open at the same time.

Dani AI

Generated

When multiple MDI children are open, the reliable approach is to ask the MDI parent which child is currently active rather than scanning children or relying only on control focus. As noted, input focus can land on a control inside a child and not clearly identify the child form. As pointed toward a parent-side property, Windows Forms exposes a built-in property for the active MDI child and an event you can handle to be notified when the active child changes (Form.ActiveMdiChild, MdiChildActivate event).

A common pattern is to handle the parent�s MdiChildActivate event and read the active child there. Example:

Private Sub MdiParent_MdiChildActivate(sender As Object, e As EventArgs) Handles Me.MdiChildActivate
    Dim activeChild As Form = Me.ActiveMdiChild
    If activeChild IsNot Nothing Then
        ' Identify by Name, Tag or cast to a specific child type
        Dim doc As DocumentForm = TryCast(activeChild, DocumentForm)
        If doc IsNot Nothing Then
            Console.WriteLine("Active document id: " & doc.DocumentId)
        Else
            Console.WriteLine("Active child: " & activeChild.Name)
        End If
    End If
End Sub

Notes: ActiveMdiChild can be Nothing if no child is active. Use each child�s Name, Tag or a custom property to identify it reliably. For classic VB (VB6) the MDI parent provides an equivalent way to get the active child; for WinForms use the property/event above for robust results.

Recommended Answers

All 2 Replies

i suppose you could see if it had focus

If your form name is "frm_MDI" you could try: frm_MDI.ActiveForm to get the current form which is active.

Hope it helped, bye.

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.