×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

(OP)
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 --> .NET

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.



What am I doing wrong?
TIA!

Regards,

Ilya

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

Well, you'll get that if you open the 'child' form when it hasn't got an MDI parent ...

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

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!

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

>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.

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

(OP)

CODE --> .NET

'====================================================================================================================================
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

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

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.

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

(OP)
To jebenson:
This approach did work!
I put together this lil' procedure

CODE --> .NET

'====================================================================================================================================
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

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

> 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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close