Building menus - Design time / Run time

  • Thread starter Thread starter muscha
  • Start date Start date
M

muscha

Hi,

I'm in a bit of indecision now, should I build my menus in design time/run
time. I feel like doing them in run time but after a while I see this
massive amount of code that put me off a bit. Feels like working in Java :P

Anyone has any input?

thanks.

/m
 
muscha said:
Hi,

I'm in a bit of indecision now, should I build my menus in design time/run
time. I feel like doing them in run time but after a while I see this
massive amount of code that put me off a bit. Feels like working in Java
:P

I'm still searching for my preferred technique, but at the moment do it at
runtime like this:

- An XML file to defines menu structure.
- I have a MenuBuilder object that reads the xml structure (recursively)
building the menu items (setting icons, shortcuts, disabling items
appropriately, etc).
- This happens in response to some event, such as when something changes.
- I'm actually using the DotNetMagic lib for menus, but it should work with
any.

I like the runtime approach becuase you can tweak the menu structure at
runtime. Also, my menu items are wired up to command objects using
reflection, so I can also tweak what menus do at runtime! Example of XML:

<menu name="File">
<menu name="Connect to Database..."
command-object="SqlBuddy2.Gui.Sessions.CmdNewConnection"
shortcut="Control,Shift,N" icon="db_connect.con" />
</menu>

I was worried that having to rebuild the menu tree time and time again might
be slow, but it's lightning fast!

There isn't that much code either, the main building function is only 52
lines of code (including comments and spaces). I also use the same function
for context menus, and a similar approach for toolbars.

All that said, I'm still learning and I'm know the approach could be
improved upon?

Tobin
 
I'm in a bit of indecision now, should I build my menus in design
time/run
:P

I'm still searching for my preferred technique, but at the moment do it at
runtime like this:

- An XML file to defines menu structure.
- I have a MenuBuilder object that reads the xml structure (recursively)
building the menu items (setting icons, shortcuts, disabling items
appropriately, etc).
- This happens in response to some event, such as when something changes.
- I'm actually using the DotNetMagic lib for menus, but it should work with
any.

I like the runtime approach becuase you can tweak the menu structure at
runtime. Also, my menu items are wired up to command objects using
reflection, so I can also tweak what menus do at runtime! Example of XML:

<menu name="File">
<menu name="Connect to Database..."
command-object="SqlBuddy2.Gui.Sessions.CmdNewConnection"
shortcut="Control,Shift,N" icon="db_connect.con" />
</menu>

That's an interesting approach. How do you approach internationalisation?
Because you set menu text directly there. The reason I'm using the run-time
because I need to get the display string from the resource file, so even if
I did it at design time I have to get my menu again and set the correct
text.

/m
 
Ahjay Muscha said:
That's an interesting approach. How do you approach internationalisation?
Because you set menu text directly there. The reason I'm using the run-time
because I need to get the display string from the resource file, so even if
I did it at design time I have to get my menu again and set the correct
text.

I haven't yet had to approach internationalisation, but I would probably
make the name of the menu item reference a hash of internationalisation
strings, that could be looked up by the MenuBuilder. I've seen this kind of
thing done in SharpDevelop. So, for example, my XML might look like this.

<menu name="File">
<menu
name="ResourceText.Menus.ConnectToDatabase.Text"
command-object="SqlBuddy2.Gui.Sessions.CmdNewConnection"
shortcut="Control,Shift,N"
icon="db_connect.con"
/>
</menu>

The MenuBuilder would the pull out the string named
"ResourceText.Menus.ConnectToDatabase" from the resources database, based on
the current language selected in the app. Code might be something like this.

MenuItem item = new MenuItem();
item.Text = Resources.GetString("ResourceText.Menus.ConnectToDatabase.Text",
"eng-uk" );

HTH

Tobin
 
Back
Top