Controlling MDI toolbars when child forms close

G

Guest

Help! Another newbie question I'm afraid. I have a toolbar on an MDI form,
which I can control OK to produce a child form. When the child form is
active, the appropriate MDI parent form toolbar button properties are set as
follows: .pushed=true and .enabled=false.

My problem is how do I set the reverse conditions when I close this child
form? I have tried putting (in the Exit button click event)
Toolbar1.Buttons(6).Pushed=False
Toolbar1.Buttons(6).Enabled=True
but get a syntax error saying that 'name Toolbar1 is not declared.'

and even
MDIParentFormname.Toolbar1.Buttons(6).Pushed=False
MDIParentFormname.Toolbar1.Buttons(6).Enabled=True
gives me a syntax error 'Reference to a non-shared member requires an object
fererence'.

How and where can I refer to the parent form toolbar within the child so
that I can change its properties? Many thanks for any help/advice given.
 
O

Oenone

LCAdeveloper said:
My problem is how do I set the reverse conditions when I close this
child form?

You need a reference to the actual instance of your MDI parent form...
I have tried putting (in the Exit button click event)
Toolbar1.Buttons(6).Pushed=False
Toolbar1.Buttons(6).Enabled=True
but get a syntax error saying that 'name Toolbar1 is not declared.'

I assume this code is within the MDI child form itself. This isn't working
because it's stating that the Toolbar1 control is within the child form,
which isn't the case. That's why it says it can't find a declaration for the
toolbar.
and even
MDIParentFormname.Toolbar1.Buttons(6).Pushed=False
MDIParentFormname.Toolbar1.Buttons(6).Enabled=True
gives me a syntax error 'Reference to a non-shared member requires an
object fererence'.

This isn't working because MDIParentFormname is a class (i.e., a description
of the MDI Parent form) rather than an object (an actual instance of the MDI
Parent form).

The easiest way to get a reference to the actual MDI Parent form object is
to use the MDIParent property of your child form. This must have been set in
order for your MDI child form to be appearing within the MDI container. So
for example, in the Closing or Closed event of your child form, try the
following code:

\\\
Me.MDIParent.Toolbar1.Buttons(6).Pushed = False
Me.MDIParent.Toolbar1.Buttons(6).Enabled = True
///

Hopefully that will do the job.
 
G

Guest

Thanks for your reply. Please forgive my ignorance, but pasting the suggested
code into my closing statement produces a syntax error in that
'Me.MdiParent.Toolbar1' is not a member of 'System.Windows.Forms.Form'.

My orginal MDI container form, let us call this MDIParent Form1, produces a
child form with the following code when a menu item is activated:
\\\
Dim NewMDIChild As New Form2
Toolbar1.Buttons(6).Pushed = True
NewMDIChild.MdiParent = Me
NewMDIChild.Show
///

I take it that this means that Form2 is an MDIChild of Form1? I'm confused
now. Do I have to declare an instance of the toolbar in the child form in
able to access it, or am I missing something more fundamental in how Toolbar1
is created? Many thanks indeed if you can help me further: I'm finding the
change from VB4 to VB.NET2003 quite a steep learning curve. :-(
 
O

Oenone

LCAdeveloper said:
Thanks for your reply. Please forgive my ignorance, but pasting the
suggested code into my closing statement produces a syntax error in
that 'Me.MdiParent.Toolbar1' is not a member of
'System.Windows.Forms.Form'.

Ah, my bad. Try this in your child form's Closing event (I've actually
tested it this time so it really should work):

\\\
Dim myMDI as Form1
myMDI = DirectCast(Me.MdiParent, Form1)
myMDI.Toolbar1.Buttons(6).Pushed = False
myMDI.Toolbar1.Buttons(6).Enabled = True
///

The first line declares a new object pointer whose type is Form1 (i.e., the
same type as your MDI parent form). It doesn't create an new object, just a
pointer to an object (which currently points to Nothing).

The second line points the myMDI variable at your MDI parent form, which it
accesses through the child form's MdiParent property. If you have Option
Strict On, this will fail with a compilation error, however, so you use
DirectCast to tell VB that MDIParent actually is of type Form1. (If
MdiParent were of a different type, this would give you a run-time error
when it executed, but as you know it will always be a Form1 object this is
ok). myMDI now points to the actual instance of Form1 that is the MDI
parent.

Now you have access to the MDI parent instance, you can access its members,
such as Toolbar1. This will then allow you to perform the updates that you
need.

Try that and see how you get on this time.
My orginal MDI container form, let us call this MDIParent Form1,
produces a child form with the following code when a menu item is
activated:
\\\
Dim NewMDIChild As New Form2
Toolbar1.Buttons(6).Pushed = True
NewMDIChild.MdiParent = Me
NewMDIChild.Show
///

I take it that this means that Form2 is an MDIChild of Form1?

Form2 is a class the describes a form, not an actual instance of a form.
NewMDIChild is an instance of a form (i.e., a real form that exists on the
screen), and it is a child of Form1 (due to the fact that it's MdiParent
property has been set to your MDI parent).
I'm
confused now. Do I have to declare an instance of the toolbar in the
child form in able to access it, or am I missing something more
fundamental in how Toolbar1 is created?

The difference is that when the MDI parent form talks to Toolbar1 in the
code you posted above, it is talking within the context of itself. The code:

Toolbar1.Buttons(6).Pushed = True

....actually in full is this:

Me.Toolbar1.Buttons(6).Pushed = True

Because you omit the "Me.", VB looks for a Toolbar1 contained within the
current object. Because this is the MDI parent and it does contain Toolbar1,
everything works fine.

However, when you try to run the same code in your MDI child form, the child
form looks for a control called Toolbar1 within itself. There isn't one
there of course, because the toolbar is in the MDI parent form, not the MDI
child form.

The fix is therefore to gain access to the MDI parent form so that you can
tell it to update its toolbar. This is what the code I posted above is
doing.

Does that make it any clearer?
 
G

Guest

Thank you very much indeed for your kind reply: your careful explanation has
made things a lot clearer for me - and the code worked too, of course! Many
thanks. :-D
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top