Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Posted

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:

 


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

Posted
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.

Glenn "Mykre" Wilson, DirectX MVP

Inner Realm

Managed DirectX and Game Programming Resources

  • *Experts*
Posted

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted
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.

Glenn "Mykre" Wilson, DirectX MVP

Inner Realm

Managed DirectX and Game Programming Resources

  • Leaders
Posted

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:

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

single-instance.zip

"These Patriot playoff wins are like Ray Charles songs, Nantucket sunsets, and hot fudge sundaes. Each one is better than the last." - Dan Shaughnessy
  • 1 month later...
Posted

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:

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?

Posted

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?

UCM

>-)

Posted

I used your code in form1 like this:

 

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....

UCM

>-)

Posted

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. :)

Posted

put this code in the menu item event you want for the creation of the mdi children:

   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?

UCM

>-)

Posted

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 :

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?

Posted

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. ???? :(

Posted

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?

UCM

>-)

Posted

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.

Posted

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:

me.parentform.frm3.dispose()

 

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

 

I'll keep thinking about it too...

UCM

>-)

Posted

Than again, another way to do it would be to put the following in the scope ( outside of any subs ):

Private frm2 As New Form2'Make sure to take off the parenthesis ()
Private frm3 As New Form3'Make sure to take off the parenthesis ()

and ignore the whole (frm3.mdiParent=Me) thing...

 

Then make a public sub in form2 that would clear the contents of form2...

 

Now you can call from anywhere in form1 to show form2 and form3...

 

then if you put in the _closing event of form2:

MyApplicationsNameHere.frm3.dispose()

then it should work much better....

 

see how I do something like this in This Thread

UCM

>-)

Posted

Minor modification to above, don't use.Dispose(), if you do then the form will dissappear permanantly and you'll have to call Private frm# as new form# again....

 

Instead, use.Hide() and .Show()...

UCM

>-)

Posted

CRACKED IT!

 

In Form2 (UserManager) I declared :-

Public frm3 as Form3
Public frm4 as form4
'etc etc

 

Then in the Activated method of Form3, form4 etc etc I have:-

frm2.frm3=Me
frm2.frm4=Me

 

Finally, in the Closing event of Form2:-

frm3.dispose
frm4.dispose
'etc etc

 

Thanks UCM. That's precisely what I needed!! Woohoo!! Happy days! :)

 

I think I'm beginning to get the hang of this OOP lark!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...