Multiple forms within a class?

fatron

Member
Joined
May 13, 2010
Messages
18
Programming Experience
1-3
Hi everyone,

I'm still learning vb.net, so I'm not terribly familiar with all the terminology, otherwise I would have searched a little harder before posting.

I'm working on a program where the user can add various forms to a MDI parent. I would like each of the child forms to be part of the same array, all of the child forms will be of various types. Some forms may display a textbox, some forms may contain a graph, some forms may contain an image, etc.

The way I'm currently doing it is to create an array for each type of form:
Public frmChildText(5) as frmTextEntry
Public frmChildPct(5) as frmPct
Public frmChildChart(5) as frmChart

frmChildText(1) = new frmTextEntry
frmChildPct(1) =new frmPct
frmChildChart(1) =new frmChart

What I would like to be able to do is something like this:
Public frmChild(5) as frmDisplay
dim frmChild(1) as new frmDisplay.frmTextEntry
dim frmChild(2) as new frmDisplay.frmPct
dim frmChild(3) as new frmDisplay.frmChart

Is something like this possible, or do I need to stick with the way I'm doing it?

Thanks
 
When the user opens another MDI child form, that form is automatically added to the form's Children collection when you set the MDI parent property and it doesn't matter what combination of your forms are in there. You of course can keep track of the individual types of forms yourself (of which I would use a List(Of FormName) for each of them instead of an array) if you want to, but ultimately for it to be displayed as an MDI child, it has to be in the Children collection.
 
Thanks for your input. Your suggestion sort of works for me. It is a good way to cycle through all the forms but I am having problems figuring out how to pass data to specific forms that way.

Right now, I'm working with something like this:

I add a child form to my project like this:
VB.NET:
DataFieldCounter += 1
        ReDim Preserve frmdataField(DataFieldCounter)
        frmdataField(DataFieldCounter) = New DataField  'type of form in my project
        frmdataField(DataFieldCounter).MdiParent = Me
        frmdataField(DataFieldCounter).Show()

Then I can cycle through all my forms like this:

VB.NET:
for x = 1 to me.mdiChildren.count
     if the child form is of a certain type, pass data to the child form
next

How would I get data or pass data to that form?
If I enter
VB.NET:
mdiChildren(x).Name
, it will return that the form is a "DataField", but I have no idea of the form's index and mdiChildren(x).textbox1.text won't actually give me access to the form's textbox.

Thanks
 
Checking for what the object is is rather easy, you'd do something similar to:
VB.NET:
For x As Integer = 1 to Me.mdiChildren.Count - 1
    Select Case True
        Case Me.mdiChildren(x) Is frmTextEntry
            'Is frmTextEntry
        Case Me.mdiChildren(x) Is frmPct
            'Is frmPct
        Case Me.mdiChildren(x) Is frmChart
            'Is frmChart
    End Select
Next
 
Thanks,
that's pretty close to what I had in mind for cycling through the forms, now, what is going to be the best way to update data on a form's control using that method?

using mdiChild(x).textbox1.text ="test" doesn't work.

Do I need to create an interface, and implement it, or is there an easier way?

Thanks.
 
You need to cast the object (the generic form) to your specific form then the properties will show up, like so:
VB.NET:
For x As Integer = 1 to Me.mdiChildren.Count - 1
    Select Case True
        Case Me.mdiChildren(x) Is frmTextEntry
            CType(Me.mdiChildren(x), frmTextEntry).Property = Whatever
        Case Me.mdiChildren(x) Is frmPct
            CType(Me.mdiChildren(x), frmPct).Property = Whatever
        Case Me.mdiChildren(x) Is frmChart
            CType(Me.mdiChildren(x), frmChart).Property = Whatever
    End Select
Next
 
Excellent. Thanks, that works.

Now, given that I don't need to access my forms through the form's array index, do I even need to create my forms as an array? The program seems to work just fine if every time I need a new form, I just create it using frmText = new frmTextEntry.
Will it cause a problem having 5 childforms all named frmText?

Thanks again, you've been very helpful.
 
Each instance will be in the children collection, so even though you're assigning all of them to frmText (which actually goes out of scope for that variable in the same sub it's created in) the pointer is kept in the Me.MdiChildren collection, which is how the forms still exist even though you lose the reference to them in your variable.

In short: No, you don't need to worry about your frmText
 
Back
Top