I would suggest storing your menu structure in some hierarchical data structure:
//C# sample
//You can replace the object initializers with constructors in the class,
//In C# 6.0, you can also create an Add extension method taking three parameters, for use with the collection initializer
// https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6#extension-add-methods-in-collection-initializers
class MenuEntry {
public string Title {get;set;}
public Action Action {get;set;}
public List<MenuEntry> Subentries {get;set;}
}
var entries = new List<MenuEntry> {
new MenuEntry() {Title = "TopLevel1", Subentries = new List<MenuEntry>() {
new MenuEntry() {Title = "SecondLevel1", Action = () => SecondLevel1()},
new MenuEntry() {Title = "SecondLevel2", Action = () => SecondLevel2()}
},
new MenuEntry() {Title = "TopLevel2", Subentries = new List<MenuEntry>() {
new MenuEntry() {Title = "SecondLevel3", Action = () => SecondLevel3()},
new MenuEntry() {Title = "SecondLevel4", Action = () => SecondLevel4()}
}
}
Then all you need is a single recursive function to render each MenuEntry
to the corresponding UI element, and connect the Action
to that element.
All else being equal, it is simplest to group the called methods (SecondLevel1
, SecondLevel2
) together, either in the same class as the main
method, or in a separate static class for all these methods. However, it may be more appropriate to keep some or all individual methods together with the appropriate parts of the application: e.g. a StartNewGame
method might belong with the Game
class.