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
"Oenone" wrote:
> LCAdeveloper wrote:
> > 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?
>
> --
>
> (O) e n o n e
>
>
>
|