0

I have tried googling ways to get an array or list of child forms of the current form to no avail. Is there a standard way to do such a thing? I am using .net frameowkr 4.0 visual studio 2013.

I have tried

For Each childForm As Form In Me.MdiChildren
    Debug.WriteLine(childForm.Name)
Next

When I know a child form is present, but I never enter the WriteLine code in my debugging. The length is 0.

Maxime Rouiller
  • 13,614
  • 9
  • 57
  • 107
codemonkeyliketab
  • 320
  • 1
  • 2
  • 17
  • You tagged `vb.net` and `c#` does it matter as the example you have provided is in `vb.net`. Also are you sure that the `Form` you are referencing does have children? The referencing form should have the property `IsMdiContainer = True`. If it does then you should be able to get all instances of children that are open to the parent (Me)... – Trevor Mar 02 '16 at 18:52
  • @Codexer A solution in either language would do fine. The conversion between the two is more or less trivial. – codemonkeyliketab Mar 02 '16 at 18:55
  • Also, what are your `Form` classes named. You could use their class names in your `For Each` statement... For example: `For Each childForm As YOURCLASSNAME In Me.MdiChildren`... but that also could throw an exception if it isn't that type... Which then you can declare it as a object and then use it... – Trevor Mar 02 '16 at 18:57
  • @Codexer Yes I could but in my experience the type of retrieval has no bearing on the length of the iterated object. I am looking for a standard way to do this type of thing in winforms, the specifics of the code are not relevant unless I am attempting the standard and failing. Additionally a more generic solution would be more favorable. – codemonkeyliketab Mar 02 '16 at 18:59
  • Can we see an example of what you're doing upon instantiation of the child forms? What you have should work as long as the forms .MdiParent property is set and this code is being ran from the parent form. – Charles May Mar 02 '16 at 19:16
  • @CharlesMay I am not using mdi forms just plain winforms. But I am giving a failed example to show what I am trying to do. – codemonkeyliketab Mar 02 '16 at 19:19
  • You are better off tracking them with your own collection see: http://stackoverflow.com/q/3751554/1070452 – Ňɏssa Pøngjǣrdenlarp Mar 02 '16 at 19:39
  • @Plutonix .net 4.0 came out 2 years after that question was asked. There may be some improvement. – codemonkeyliketab Mar 02 '16 at 19:48
  • They havent. It is still present in NET 4.5. if you are writing a forms-centric app, it is pretty trivial to add a collection to track the open ones. – Ňɏssa Pøngjǣrdenlarp Mar 02 '16 at 22:43

1 Answers1

0

OK, what I THINK you're wanting is to look at the Application.OpenForms collection.

Form1 instantiates a new instance of Form2

Dim f2 as New Form2
f2.Name = "foo"
f2.Show

Then in form1 you might be able to get what you're wanting with

For Each child as Form in Application.OpenForms.OfType(Of Form2)()
    Debug.WriteLine(child.Name)
Next
Charles May
  • 1,725
  • 1
  • 11
  • 18
  • unfortunately I think you are using a later .net framework. It says "OfType is not a member of System.Windows.Forms.FormsCollection." But this looks like what I want as long as each form in the collection is a child of the form you are calling the for each from. – codemonkeyliketab Mar 02 '16 at 19:35
  • Yea, totally missed the 4.0 framework limitation. This however does not care which form instantiated form2 it is only pulling forms of type Form2 from the collection so it's probably still not what you're seeking. I have to ask, how would you be able to differentiate if form1 instantiated an instance of form2 vs form3 instantiating an instance of form2? I don't think there's a container of who instantiated who but I would think you could create a List(Of T) and add to it each time you instantiate. – Charles May Mar 02 '16 at 19:48
  • Yes a list would be a solution, but to answer your question, when I open form B0 from A and form B1 from C (B0=B1), when I close A only B0 is closed. C and B1 remain. I was hoping to reproduce this functionality, that winforms have inherently, in code without having to use lists to track it myself. – codemonkeyliketab Mar 02 '16 at 19:51