Changing Parent Items From Child

How can I change parent items such as menu items from a child?
[62 byte] By [codefund.com] at [2007-12-16]
# 1
Check out the form's MDIParent property. Returns a reference to the parent form.
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2
Thanks! That helped a ton!
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
If I wanted to reference a subroutine in my parent, how would I do that?
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4
Just make sure the procedure is Public, then you can call

Me.MdiParent.SubRoutine

from the child form.

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5
I believe you'll have to cast the form to the exact type of form that it is though to get it to compile ;)

CType(Me.MdiParent, MyForm).SubRoutine()

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 6
Well, I can't get it to work right. Here is my code from the parent: Public Sub mnu_Changes()
mnuPrint.Visible = (Me.MdiChildren.Length > 0)
mnuPrintPreview.Visible = (Me.MdiChildren.Length > 0)
mnuPageSetup.Visible = (Me.MdiChildren.Length > 0)
mnuSpacer2.Visible = (Me.MdiChildren.Length > 0)
mnuWindow.Visible = (Me.MdiChildren.Length > 0)
End SubAnd from the child: Private Sub closeMe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles closeMe.Click
Me.MdiParent.mnu_changes()
End Sub
Sorry for being a pain. :}
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 7
After reading Erik's post, here is my new code: Public Sub mnu_Changes()
mnuPrint.Visible = (Me.MdiChildren.Length > 0)
mnuPrintPreview.Visible = (Me.MdiChildren.Length > 0)
mnuPageSetup.Visible = (Me.MdiChildren.Length > 0)
mnuSpacer2.Visible = (Me.MdiChildren.Length > 0)
mnuWindow.Visible = (Me.MdiChildren.Length > 0)
'MessageBox.Show("The mnu_Changes sub has been entered")
End SubAnd the child: Private Sub EditConfig_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Closed
CType(Me.MdiParent, ConfigThis).mnu_Changes()
End SubWhether I use Closed or Leave, the child is still open and cannot change the menu.
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 8
First of all, don't retrieve a property more than once. This is inefficient, in any programming language or environment that I'm aware of. Better to use something like this:

Public Sub mnu_Changes()
Dim blnEnable As Boolean = (Me.MdiChildren.Length > 0)
mnuPrint.Visible = blnEnable
mnuPrintPreview.Visible = blnEnable
mnuPageSetup.Visible = blnEnable
mnuSpacer2.Visible = blnEnable
mnuWindow.Visible = blnEnable
'MessageBox.Show("The mnu_Changes sub has been entered")
End Sub

And, in another thread, we talked about how you could run this code when the user clicked a menu item on the parent. There's no reason I can imagine where you'd want to run this code from any other place. There's no reason to do this anywhere but when you're about to drop down the menu, and that code looks like this (from the other thread):

Private Sub mnuFile_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuFile.Click
mnu_Changes()
End Sub

In other words, why are you attempting to call this code from a child form? The child form can't affect the state of the menus, and since you want to determine the state of the menu items at the moment you display the menu, there's no point doing it elsewhere.

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 9
>>I believe you'll have to cast the form to the exact type of form that it is though to
>> get it to compile

>> CType(Me.MdiParent, MyForm).SubRoutine()

That's what you get for writing "air code"! Oops. Sorry. <g>

On the other hand, I would suggest using DirectCast rather than CType (it's just a good habit to get into, for converting reference types where the conversion can't fail) in cases like this. Supposedly, DirectCast gives slightly better performance, 'cause it doesn't try to do any actual conversion, just coercion.

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 10
It's not the conversion issue per say. It's the call of the method. It's being called before the child is released. How can I act against that?
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 11
<quote>
On the other hand, I would suggest using DirectCast rather than CType (it's just a good habit to get into, for converting reference types where the conversion can't fail) in cases like this. Supposedly, DirectCast gives slightly better performance, 'cause it doesn't try to do any actual conversion, just coercion.
</quote>

true...i keep forgetting to get into that habit :(

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 12
Ken,

I understand your point about the properties. I have made that change since posting my last sample.

As for the second part, I am trying to change the menu items to be hidden again when all child windows are closed. How can I do this effectively?

Jason

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 13
Jason, I think what Ken is trying to say is that there's no need to update the menu when the child form closes, because the user will not actually "see" the difference in the menu until they actually click on the file menu, so why not just wait to change the sub menu items until they actually click on it.
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 14
First of all, the menu items aren't going to be hidden, right? They're disabled.

And, they will be disabled if there aren't any children. That's the point. You update the availability of the menu items each time you pull down the menu, so that if there aren't any open children, the menu items won't be available.

Did you actually try it?

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...