Multiple Forms

lisk

Newcomer
Joined
Jan 10, 2003
Messages
1
In my solution Explorer, I ve got form1 and form2
On form1 I`ve got a button1. What I want to do is: when i click button1, form2 should "pop up" How do I do that?
 
Visual Basic:
Dim f As Form2 = New Form2()
f.Show()

Should do it, assuming this code is placed in Form1.
 
How would you make sure that only one instance of each form is open at a time. Could you please post some sample code.
 
I`m not sure I understand what you mean. My problem was that i had in Project1 two forms; form1 and form2. On form1 I would have a button, button1. When this button were clicked , form2 would open. So on form, I implemented the code:

Visual Basic:
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        Try
            Dim x As form2= New form2()
             x.Show()
            x.Size = New System.Drawing.Size(648, 456)
        Catch ex As OleDb.OleDbException
            MessageBox.Show(ex.Message)
 End Try
end sub
 
I am creating an MDI Application and need to make sure that when a child form is created there is only allowed to be one instance of it. For example the user selects the about menu item, and at the moment if the user selects it again with out closing the previous one, a new form is created. What I would like is the old from to regain it's focus instead of creating a new form.
 
Do you want only one CHILD window open at once, or one modal window open at once? Normally, an About box is a modal window. If you use ShowDialog() instead of Show(), it will open modally and that pretty much forces one instance at a time (unless you allow frmAbout to open itself :))

If you need to prevent child windows, ask again :)

-nerseus
 
Sorry bad example, yes I do only want one of each child open at a time. I have had one person say to look through the mdichildren collection of the parent, but I do not know how to.
 
I have just been playing around here a little and it seems I have come up with a solution for you. I'm not sure if it is the best solution or not since I'm new to .NET myself. The heart of the code is as follows in the MDI Parent form:
Visual Basic:
Private Sub mnuChild2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuChild2.Click
    Dim frm As New Child2()
    If Not IsOpen(frm) Then
        frm.MdiParent = Me
        frm.Show()
    End If
End Sub

Private Function IsOpen(ByVal frm As Form) As Boolean
    Dim ret As Boolean, stdForm As Form
    ret = False

    For Each stdForm In Me.MdiChildren
        If stdForm.Name = frm.Name Then
            stdForm.Focus()
            ret = True
            Exit For
        End If
    Next

    Return ret
End Function
I am attcahing the project for you to look at.

Orbity
 

Attachments

I also have an MDI question.

I have a Main form (we'll call it form 1)
From form1 I have some code that creates two new forms (form2 and form3)

I want form2 to be the parent of form3.
I am using this code to set the properties. (form2 has it's IsMDIContainer set to True at desing time).

on Form1:
Visual Basic:
dim frm2 as new Form2
dim frm3 as new form3

frm3.mdiParent=frm2
frm2.show
frm3.show

Form2 does indeed display itself, Form3 does not. any ideas?
 
When I use your code, I get the following form display

are you trying to get form3 to be inside of form2 and also to get form2 inside of form1? or are you going for something else?
 

Attachments

I used your code in form1 like this:

Visual Basic:
Dim frm2 As New Form2()
Dim frm3 As New Form3()


  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
frm3.MdiParent = frm2
frm2.Show()
frm3.Show()
  End Sub

I also set the IsMdiContainer property in both form1 and form2 to true....
 
Thanks UCM.

That's not what I'm trying to achieve.

When a menu item is clicked on Form1, it opens up Form2 and Form3. I don't want Form3 to be inside Form2.

When I close Form2, I also want Form3 (and any other child forms) to be closed as well, just leaving Form1.

Hope this makes sense. :)
 
put this code in the menu item event you want for the creation of the mdi children:
Visual Basic:
    Dim frm As New Form3()
    frm.MdiParent = Me
    frm.Show()

As for closing all of the mdichildren forms, you can create a loop to close each one of them using the Me.MdiChildren array...you could put it in the closed event of form3 then this way, it wouldn't matter which one of the child forms was closed, they would all close...

The only issue with that would be that you'd need to specify a way to skip the loop if the closing of all child forms process is active...

Sounds like fun, heh?
 
I don't think that's correct.

My MenuItem lives on Form1 and is responsible for creating instances of Form2 and Form3 when clicked.

I want Form2 to be the parent of Form3.

If I specify :
Visual Basic:
Dim frm2 as new Form2()
Dim frm3 as new Form3()
frm3.mdiParent=Me
...bearing in mind that this code lives on Form1, wouldn't this set Form1 to be Form3's parent?
 
Also,

I've set a button on Form2 to display the name of it's MDIChildren.
It comes back to say the Form3 is a child, so it is assigning it correctly.

However, Form3 never displays itself after the Form3.Show method is called. ???? :(
 
hmmmm....

Ok so you want form1's menuitem event to open form2 and form3...you don't want form3 to be inside of form2, but you want form2 to be the parent of form3....

I'm just a little confused :confused: ...do you want form2 to be opened inside of form1 but have form3 open as a new independent form outside of form1?

what is the reason for having form2 be the parent of form3?
 
OK.

Form 1 acts as a control panel in my application.
Form 2 is a replica that I've created of the NT User Manager.
Form3 lists the currently selected users Shortcuts and profile information etc etc.

When a button/menu item on Form1 (controlpanel) is clicked, an input box is displayed asking for a user id.

Form1 then creates an instance of Form2 (user manager), not within Form1, but seperate, displaying that users name, description, password info etc.

After that, Form1 creates an instance of Form3, again, seperately, that displays that users profile and shortcut information.

I want Form3 to be a child of the User Manager form, so that when the User Manager form is closed, Form3 is closed too.

Hope this makes more sense UCM and thanks for your help so far.
 
Perfect, ok then...since form2 and from3 are templates that you create new instances of, you can try this code, I'm not sure if it'll work ( i'm at work right now and away from my vb ), put in the _closing event of form2 something like:
Visual Basic:
me.parentform.frm3.dispose()

try changeing the menuitem sub to "Public" first and see if that worx....

I'll keep thinking about it too...
 
Back
Top