PC Review


Reply
Thread Tools Rate Thread

Controlling MDI toolbars when child forms close

 
 
=?Utf-8?B?TENBZGV2ZWxvcGVy?=
Guest
Posts: n/a
 
      31st Mar 2005
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.
 
Reply With Quote
 
 
 
 
Oenone
Guest
Posts: n/a
 
      31st Mar 2005
LCAdeveloper wrote:

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

--

(O) e n o n e


 
Reply With Quote
 
=?Utf-8?B?TENBZGV2ZWxvcGVy?=
Guest
Posts: n/a
 
      31st Mar 2005
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. :-(

"Oenone" wrote:

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

 
Reply With Quote
 
Oenone
Guest
Posts: n/a
 
      31st Mar 2005
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


 
Reply With Quote
 
=?Utf-8?B?TENBZGV2ZWxvcGVy?=
Guest
Posts: n/a
 
      31st Mar 2005
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
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to close the parent and all child forms together at once when click on parent window's close button X. VBLearner Microsoft VB .NET 2 25th Sep 2006 05:57 AM
close child forms recursicely Birgit Microsoft Dot NET Compact Framework 0 28th Apr 2006 04:46 PM
Close all child forms =?Utf-8?B?QnJ1bm8gUm9kcmlndWVz?= Microsoft C# .NET 2 23rd Oct 2005 02:51 AM
Close all child forms K.K. Microsoft VB .NET 3 16th May 2005 01:06 PM
Close Child Forms Rudolf Ball Microsoft Dot NET Framework Forms 3 17th Sep 2003 02:16 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:02 AM.