Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How many child SDI forms are open in a parent MDI form? 1

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
571
US
Colleagues,
I need to know, before opening (by selecting item in the bar menu) how many other forms are open in this parent MDI form.
I tried to do it in the child (SDI) form, in the Form_Load() event.
Since the argument "sender" in the Form_Load(sender As Object, e As EventArgs) event points on its own form (as hovering mouse pointer over "sender" in debug/break run has shown), not the calling one, I tried something like

Code:
For Each loForm As Form In Me.MdiParent.MdiChildren
   If loForm.Name <> Me.Name Then loForm.Close()
Next

and got an "Unhandled Exception" popup pointing on the Me.MdiParent.MdiChildren part.

20200520_MdiChildrenErrs_lmzkbe.jpg


What am I doing wrong?
TIA!

Regards,

Ilya
 
Well, you'll get that if you open the 'child' form when it hasn't got an MDI parent ...
 
It seems you should do this in the code that opens the child form, not in the child form's load even handler.

For example, say this is the code to open a new child form, FormChild, from the parent:

Code:
Dim frmChild As FormChild

frmChild = New FormChild

'here, Me is the MdiParent
For Each loForm As Form In Me.MdiChildren 
   If loForm.Name <> frmChild.Name Then loForm.Close()
Next

If the child is being opened by another child, then this:

Code:
Dim frmChild As FormChild

frmChild = New FormChild

'here, Me is the MdiChild that is opening the new MdiChild form
For Each loForm As Form In Me.MdiParent.MdiChildren 
   If loForm.Name <> frmChild.Name Then loForm.Close()
Next

Be careful with the second one, because it could close the child form that is running the code that opens the new child form. You probably need to throw in a check in the For Each loop to ignore the current form, then close that form after the new form is added to the MdiParent.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
>not in the child form's load even handler

Huh? Works fine in the Load event. If the form actually IS an MDIChild at that point.
 
Code:
'====================================================================================================================================
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'====================================================================================================================================
' The real forms' names are "obfuscated"
   If Me.IsMdiContainer Then
      frmTen.MdiParent = Me
      frmOne.MdiParent = Me
      frmTwo.MdiParent = Me
      frmThree.MdiParent = Me
   End If
End Sub
'====================================================================================================================================

and frmMain is MDI container (set on design time).

What am I missing?

Regards,

Ilya
 
Simple to check. Just add

MsgBox(Me.IsMdiChild)

before where your error occurs. Or run in the immediate window when the error occurs. Or set a watch on IsMdiChild. I'll bet it says 'False'

And without seeing pretty much all the rest of your MDI handling code I cannot begin to suggest what you may have missed. Object lifetime may be one of the things. If I had to hazard a guess I'd suggest that you've closed the MDiChild at some point.
 
To jebenson:
This approach did work!
I put together this lil' procedure

Code:
'====================================================================================================================================
Private Sub CloseAllChildren()
'====================================================================================================================================
' Purpose       : Closes all the currently open child forms.
' Description   : Checks the children SDI forms presence, if any - closes them.
' Side effects  : Shan't be any.
' Notes         : 1. Application-specific.
'                 2. Complies with .NET Framework ver. 1.1 and higher.
' Author        : Ilya I. Rabyy
' Revisions     : 2020-05-20 by Ilya – completed and Beta-tested 1st draft.
'====================================================================================================================================
For Each loForm As Form In Me.MdiChildren
   loForm.Close()
Next

End Sub
'====================================================================================================================================

and posted it in all the XXXToolStripMenuItem_Click() subs B 4 calling another form, and it proved workable.
(One minor inconvenience: it closes currently shown form no matter what when I click on any bar menu item, but that's OK, they can live with it.)

Thank you, colleague!


Regards,

Ilya
 
> B 4 calling another form

And how are you 'calling another form'?

I ask because I rather suspect (although I could be wrong)

>Side effects : Shan't be any.

may not be entirely accurate, and the root cause of your initial problem
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top