Looping Through Multiple Forms

slackey26

Member
Joined
Dec 5, 2005
Messages
5
Location
Missouri
Programming Experience
3-5
Ok, I'm new to VB.NET and I'm having a slight problem. On my main form F1, I have a list box with items in it. Double clicking on an item creates a new instance of the second form F2 with a text box that holds the value of the list box that was selected. I want to be able to loop through the forms to see which form holds a certain text box value.

I have thought of maybe using arrays but figured there has to be a collection of the forms somewhere that I could use a for each loop for.

Any help would be greatly appreciated.
 
Hi slackey26, I really don't know if there's a collection of forms. But, if each form opens from only one form, then you may take a look at OwnedForms and Owner properties. First of them is a collection of forms wich were defined as "child forms". For doing it, you may have a code like this:

Dim Form2 As New Form2()

Form2.Owner = Me 'Me of course is refering to the main form

Form2.Show()


So, at OwnedForms property of the main form, you can loop through each form that you have added to this collection doing what I put above.

I hope that helps and can solve your trouble.
Regards and good luck!.
 
Owned forms exist for a particular reason and if that reason doesn't exist in your app then owned forms is not appropriate just so you can get a collection of forms. VB.NET is an OO language so there doesn't just have to be a collection of forms. In OOP, if you want an object then you create one. If you're opening all these forms from one main form then you can just create your own collection. Just give your main form a member variable of type ArrayList and Add a reference to each newly opened form to it. If you're keen, you can create a strongly-typed collection of Forms. If you're using VB 2005 this is dead easy with a Generic.List(Of Form), but in older versions it means inheriting from CollectionBase and is probably not worth the effort.
 
jmcilhinney said:
Owned forms exist for a particular reason and if that reason doesn't exist in your app then owned forms is not appropriate just so you can get a collection of forms.

What are you refering to when you say owned forms exist for a particular reason? It sounds interest to me, I'm just curious.

Have a good day.
 
mariano_donati said:
What are you refering to when you say owned forms exist for a particular reason? It sounds interest to me, I'm just curious.

Have a good day.
When a form is owned it has a particular behaviour, specifically it is a modeless dialogue. An owned form will always appear on top of its owner, but it will not prevent the owner from receiving focus. It will also be minimised, restored and closed along with its owner. The VS.NET Find and Replace dialogue is an example of an owned form.
 
Well I really didn't know that, thank you very much for your explanation. I finally got the idea of that phrase. Again, thanks for clarify that.
Regards.
 
Back
Top