Multiple child forms

Tony

Member
Joined
Jan 8, 2008
Messages
6
Programming Experience
1-3
Hi everyone!

I'm having a bit of a problem, and I wonder if anyone could help me. I've looked online all morning in various forums but I'm yet to find any answers.

Basically, I've got a parent (MDI) form with a File menu. On the File menu there is a sub-menu item called New - which, as I'm sure you've guessed, opens a child form. The child form basically allows you to set options before which you press a button called btnStart, which instatiates various custom objects held in memory (including data from a DB). All of this is not a problem, and works as intended!

However, I need to have multiple instances of the same child form opened and 'running' in my application - and this is where the problem sems to arise, in that only the first child form seems to work. Or at least, only one child form seems to work.

Please, please, please can someone help me with any pointers and help.

Cheers.
 
What I mean by 'work' or 'not work' is this: one child form on it's own, does exactly what i want it to do, which is instatiate several custom classes, make connections to a database, send and receive data, call custom functions, etc. But when I have more than one child form 'running' (and each child form is instantaited in the same way, ie,

VB.NET:
Private Sub mnuOpenNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOpenNew.Click
        Dim newWin As New NewMainForm()
        newWin.MdiParent = Me
        newWin.Show()
        newWin.Text = "Form " + Count.ToString()
        Count = Count + 1
    End Sub

only one form 'works'... I can't see why this should be.
 
Last edited by a moderator:
interesting... What code is in the child form itself? Personally, I'd have rewritten your code (it's fine btw, this is just personal preference) to;
VB.NET:
Dim newWin As NewMainForm = New NewMainForm
newWin.MdiParent  = Me
newWin.Show()
newWin.Text = "Form " & Count
Count += 1
But as I said, ,that's just me for personal preferences. There is optimization reasons for the first line in the declaration in that declaring it explicitly like I did, the compiler does not have to check each time you reference newWin that the object is created - the declaration and instanciation is done on the same line. Anyway.... can you post the code you have in NewMainForm?
 
Cheers for the 'optimization' tip! Back to the main problem... Just a thought here.... each time I create a new child form, using the code above, does vb.net treat each child form (and therefore all the datasets, connections and custom objects floating around in memory) as seperate entities - ie, not getting confused by each child form's objects, datasets, etc? Because each child form's name/id = newWin, even though they are new sperate forms?
 
well.. it kind of depends on the code within NewMainForm itself but the theory is that it *should* create new instances of dataset etc provided you're not sharing stuff but it really all depends on how you're going about it which was why I was asking to see the code. The likes of controls and that should be instanciated with each new NewMainForm but any code where you're setting properties etc might be using shared references. I'm inclined to think the later given your issue
 
I understand but it's hard to debug something you can't see. My advice would be to try and go through the object references one by one and try and find what's being passed between instances of the forms
 
I've finally managed to find a solution to my woes.... nested classes. I realise that this might not work for all scenarios, but in mine it works a treat. This way, each child form definitely works independantly of every other child form. if anyone knows a better solution, post it here and I'll be happy to take the advice.
 
Back
Top