From: Andy on
Some time ago, I had some very useful advice from this newsgroup, regarding a
problem I had at that time, and perhaps I should summarize that issue first,
so you know why I have a new problem.

I had an app with many MDI child forms, each one dedicated to maintaining a
specific object type. There was a lot of code repeated across all of the
forms, for managing the appearance and behaviour of (1) a TreeView which is
used to select which set of properties is to be edited, (2) an array of
Frames, each of which contained the necessary controls for one set of
properties, (3) a pair of ScrollBars which could be used to move the current
Frame within a PictureBox - this gave me a ViewPort effect, so that if the
form was resized below the necessary size for displaying a Frame in its
entirety, the user could still get to access any control contained within it.
I wanted to turn this repeated code into a UserControl, and to replace the
Frames with other UserControls, one for each set of properties (since these
were also repeated in many cases across the various different objects managed
by the forms.)

The advice I got has shown me how to host a UserControl (for a set of
properties) inside another (my 'Sizable Container'). Also, I discovered how
to dynamically load these 'hosted' UCs into the host container at run-time.
As a consequence, I am approaching the point where I only need one MDI child
form, having factored out all the controls and associated code specific to
each set of properties, and I have a UC for each set. At runtime, the form
is loaded, and then configured (the necessary UCs are sited on it and
initialized). I am happy that it all works fine - and it fits with my desire
to remove duplicated code by factoring everything out into UCs. Code reuse -
you can't beat it.

OK - now the new problem I have. My existing forms had different menus.
Broadly speaking, there was a common set of menus for managing the forms
(e.g. File-Exit, Window-Tile Horizontally, etc.) and other menus and items
specific to each set of properties. All the necessary menu items were
provided on each form, and when a new Frame was brought into view (within the
NodeClick event of the TreeView), the relevant menu items became visible. It
looks like I can't do that any more with my new form architecture. I need to
add menu items dynamically at the time I configure the form, and furthermore,
I need to associate each one with a method in the corresponding UC. The UCs
will also need to access the menu items' properties, partiicularly to
enable/disable them, and modify their captions.

How am I going to achieve this?
From: Steve Gerrard on

"Andy" <andrewjellis(a)hotmailnospam.com> wrote in message
news:FD2B5D08-EECF-42C4-A194-ADDA10314AEC(a)microsoft.com...

>
> OK - now the new problem I have. My existing forms had different menus.
> Broadly speaking, there was a common set of menus for managing the forms
> (e.g. File-Exit, Window-Tile Horizontally, etc.) and other menus and items
> specific to each set of properties. All the necessary menu items were
> provided on each form, and when a new Frame was brought into view (within the
> NodeClick event of the TreeView), the relevant menu items became visible. It
> looks like I can't do that any more with my new form architecture. I need to
> add menu items dynamically at the time I configure the form, and furthermore,
> I need to associate each one with a method in the corresponding UC. The UCs
> will also need to access the menu items' properties, partiicularly to
> enable/disable them, and modify their captions.
>

You may be pleased to learn that UserControls can have their own menus.

In the UC designer, you can click the Menu toolbar button, and design a menu,
just like for a form. It won't be visible on the UC, however. As you create each
menu item, designate it as "negotiate position Right" in the drop down for that
purpose.

In the code of the UC, you can select the menu items in the upper left drop
down, and add code for their click events.

On the host form, make the menu items "negotiate position left", and set them up
as usual.

At run time, the menu of the form will contain its own menu items, followed by
the menu items of the user control.

User Controls rule.



From: Andy on
"Steve Gerrard" wrote:

> You may be pleased to learn that UserControls can have their own menus.
I was aware of that - but I can't get them to behave as I would like.

The instructions I have snipped from your reply work fine - but only if I
put a menu on the host UC, which isn't what I need. If I put a menu on the
hosted UC, i.e. at a lower level, it doesn't get propagated up to be
displayed on the form's menu bar. Perhaps this is connected with the fact
that I am siting the hosted UC at runtime?

Further - I would like to place the UC's menus in the middle of the form's
menu bar. It's usual to see the File menu at the left, the Window and Help
menus on the right, and everything else in-between. Assuming I can get the
propagation issue sorted out, should I achieve this ordering by setting the
File menu to negotiate left, the Window and Help menus to negotiate right,
and all the UC's menus to negotiate middle?
From: Steve Gerrard on

"Andy" <andrewjellis(a)hotmailnospam.com> wrote in message
news:B00D5578-ED60-48F6-B4B5-B6987E8526DE(a)microsoft.com...
> "Steve Gerrard" wrote:
>
>> You may be pleased to learn that UserControls can have their own menus.
> I was aware of that - but I can't get them to behave as I would like.
>
> The instructions I have snipped from your reply work fine - but only if I
> put a menu on the host UC, which isn't what I need. If I put a menu on the
> hosted UC, i.e. at a lower level, it doesn't get propagated up to be
> displayed on the form's menu bar. Perhaps this is connected with the fact
> that I am siting the hosted UC at runtime?
>

I can't make a nested UC pass its menu up through a host UC to the outer Form,
even at design time. :(


> Further - I would like to place the UC's menus in the middle of the form's
> menu bar. It's usual to see the File menu at the left, the Window and Help
> menus on the right, and everything else in-between. Assuming I can get the
> propagation issue sorted out, should I achieve this ordering by setting the
> File menu to negotiate left, the Window and Help menus to negotiate right,
> and all the UC's menus to negotiate middle?

This does work for the UC hosted directly on a Form.

I haven't really used this feature, I just thought it might be handy. It does
seem a little limited, though. Oh, well.

I will poke around for a way to just load the menu at runtime...



From: Andy on
"Steve Gerrard" wrote:

> I will poke around for a way to just load the menu at runtime...

I'd be very grateful for that, thanks. Don't know where you are, but here
(in UK) it's 4:27am, and I can't keep myself going any longer, so there's no
rush!

BTW - I need to do Popup menus as well. I did find this, which I could use
if necessary...
http://www.developerfusion.co.uk/show/5475/

Don't know if it's reworkable for menus on forms, though.
 |  Next  |  Last
Pages: 1 2 3
Prev: COM-RPCServer - New Version
Next: Debug VB OCX and DLL