Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
Hi. I am trying to allow a button on a child form to call another child form. I assume I would have to create a handler that would refer to the child form to be opened and place it on the parent form. I am inexperienced with writing handlers. Does anyone have an example or a link to one? Thanks!
  • *Experts*
Posted

You could make sure that the variable you create on your parent to call that another child is public, and will be accessible from another class. Then cast the MDIParent property of the current child to an actual class name of your parent form and then access the variable you create for that other child like this:

DirectCast(Me.MDIParent, Form1).OtherChild.Show()

Other simple way be to store your insatnces of all children as shared in a separate class.

Posted (edited)

I got it. Thanks a lot mutant. I had to add the following line before the line you gave me:

 DirectCast(Me.MdiParent, Form1).edit.MdiParent = Me.MdiParent

You're a lifesaver!... I just realized I have another problem now. If I close the child window I just created, I can't reopen it because it says that it cannon access a disposed object. I'm sure this is simple but I don't understand the problem. Thanks for your time!

Edited by martialarts
  • *Experts*
Posted

I assumed that you already set the MdiParent property for the child, thats why you have to add that line because you probably didnt assign a parent eariler.

 

By using .Close() method of the form you are disposing it.

Do you want to preserve the instance of your child so you only have one, or with each opening you want to create a new one? If you want one then instead of closing the form hide it, and then show it when you need it. If you wat new instance each time you want to open it then simply create a new instance on that variable before opening it.

Posted
I clicked close in the control box... it is the only way to close it. Is there a way I can create a handler for the close button in the control box? I guess it doesn't really matter if I reuse the form or create a new instance each time.
  • *Experts*
Posted

Oh, I thought you would have some code in that form that would hide itself. If you dont mind creating new instances then before opening it simply initialize a new instance:

DirectCast(Me.MdiParent, Form1).edit = New edit
DirectCast(Me.MdiParent, Form1).edit.MdiParent = Me.MdiParent
DirectCast(Me.MdiParent, Form1).edit.Show()

  • *Experts*
Posted

Btw. for this whole thread I was assuming that you want to work with that instance of the form from the parent too, and from your other child you just wanted to do something to that instance, in other words you wanted to use that instance in both parent and another child. Is that right? If you dont need to work in the parent with that child you show from the other child then there is no need all this, simply showing creating a new local instance and showing it would work.

Just thought I would ask :)

Posted

Everything works great now! Thanks a lot. I use the parent form strictly as a container with a menu so I don't need interaction with it. So I should be able to call it simply by using this code?:

 Dim main As Form1
           Dim editable As New Form3()
           editable.MdiParent = Me.MdiParent
           editable.Show()

I just noticed I had used me.parent instead of me.mdiparent.

  • *Experts*
Posted

Oh, well I assumed too much :)

But maybe that code could become of some use to you in the future.

Yes, that would be all you need. Sorry for wasting your time with my assumptions, when I could simply write what you just typed up there.

Posted
Every time I think I'm done, I realize I'm not. How do I make sure I don't open the same form twice. From what I can gather there is a child collection (mdichildren?). How do I access it and confirm that I'm not loading multiple instances of a form at the same time. Thanks!
  • *Experts*
Posted

This is where the code I showed you before would work. Having one variable with an instance in the parent that all children can access, everytime you would call the Show() method of the instance that is stored in that variable it would show only that form, it wouldnt create a new instance.

 

Other way would be too go through all the form in MDIChildren collection like you mentioned and check what form it is:

Dim f as Form
For Each f in Me.MdiChildren 'Me here refers to the parent, if calling from child
'form use Me.MdiParent.MdiChildren
If TypeOf f Is SomeFormClass Then
'form of the type already opened
End If
Next

 

The first thing would give you more control over all this, then checking for the form type in the child collection.

Posted
Does the first situation bring me back to the issue of receiving errors when I reopen forms because they were already disposed... after all, I close the forms with the close button in the control box.
  • *Experts*
Posted

You could put this code in the Closing event of your child:

e.Handled = True 'stop the closing
Me.Hide() 'instead of closing, hide the form

This way you will have only one instance of the form.

Posted (edited)

I just tried your recommendation. I am getting the error:

'handled' is not a member of 'System.componentmodel.canceleventargs'.

Do you know why I am getting this? Also, I am trying to call one of the children from a modal dialog and none of the children are open... how can I address the mdiparent? Thanks.

Edited by martialarts
  • *Experts*
Posted

Im sorry, its supposed to be e.Cancel.

Are you showing the dialog box from the parent? If you show it from the parent, when you show it with ShowDialog() pass in the parent as the owner of the ShowDialog(Me). THis way you can refer to the parent with the Owner property from the dialog.

Posted
It accepts the e.cancel... I still have the other problem. I might just be getting confused... but I think I miscommunicated. I am trying to call a child from a modal. So the modal isn't a child and there aren't any child windows open. I need the modal to tell the parent to open the child... I think:confused: I tried several variations, including this:
            Dim main As Form1
           DirectCast(main, Form1).input.MdiParent = main
           DirectCast(main, Form1).input.Show()

Form1 is the class of the parent form, main is the instance of the parent form, and input is the child form I am trying to show. Thanks!

  • *Experts*
Posted

That would work with a slight modification if you did what I said in my eariler post. When you show the dialog form from the parent pass in the reference to the parent into the ShowDialog method:

modalform.ShowDialog(Me)

Then you would do this in the modal form:

Dim main As Form1 = Me.Owner
main.input.MdiParent = Me.Owner
main.input.Show()

Or using casting:

DirectCast(Me.Owner, Form1).input.MdiParent = Me.Owner
DirectCast(Me.Owner, Form1).input.Show()

Posted
That worked great. I'm glad you always have the answers. I wish I knew the big picture better so I could figure this stuff out. I think I have most of the bugs worked out of my mdi now. How do I make the close button in the control box of the parent end the program? How do I disable certain features on forms when I open/close other forms? Sorry to ask you so many questions. I have books and I search online, and in the vb.net help, but none of them are helpful on mdi issues.
  • *Experts*
Posted
The problem with closing the parent when the closing event of children is edited in not a really good feature of MDI apps. What you can do it create another public variable in your parent of boolean type and set it initially to False. Then in the closing event of the parent set it to true, and in the closing events of the children instead of hiding them right away do a check for value of that boolean variable on the parent. If its false then just hide it like you would do normally, if true then do nothing there.

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