Getting the memory function to work was very similar to setting up the buttons so I didn't need to look up anything new. I had never seen menus for .NET yet so I did need to look up a couple things. Overall, however, it was extremely simple and didn't take much time at all.
As one would expect, rendering menus is very repetitive. I will only detail the "View" menu since that required a "checkable" menu item which makes it more interesting.
- Rendering the menu
- XAML:
<menu dockpanel.dock="Top" height="26"> ... <menuitem header="View"> <menuitem name="StandardMenu" click="OnMenuStandard" ischeckable="true" ischecked="True" header="Standard"></menuitem> </menuitem> ... </menu> - C#:
Menu menu = new Menu(); DockPanel.SetDock(menu, Dock.Top); menu.Height = 26; ... MenuItem view = new MenuItem(); view.Header = "View"; StandardMenu = new MenuItem(); StandardMenu.Name = "StandardMenu"; StandardMenu.Click += new RoutedEventHandler(OnMenuStandard); StandardMenu.IsCheckable = true; StandardMenu.IsChecked = true; StandardMenu.Header = "Standard"; view.Items.Add(StandardMenu); menu.Items.Add(view); ... MyPanel.Children.Add(menu);
- Notes:
The menu bar is set up first and is defined to be docked to the top of the window's DockPanel. The DockPanel was set up a long time ago to hold the grid. The grid was not docked so I guess DockPanel automatically places other children one after the other if they are not explicitly docked to a certain place. Here, the menu is docked to the top with
DockPanel.SetDock(menu, Dock.Top);. I had a little trouble finding that but the .NET Framework Class Library pointed me in the right direction. My other problem was how to add children to the menus because there are a lot of collections and "add" methods to the "MenuItem" class. I was told "The submenu of the MenuItem is made up of the objects within the ItemCollection of a MenuItem." but I had to do some digging to discover "Items.Add(...)" was the way to go.
- XAML:
- Making it do something
- C#:
void OnMenuStandard(object sender, RoutedEventArgs e) { //((MenuItem)ScientificMenu).IsChecked = false; ((MenuItem)StandardMenu).IsChecked = true; //for now always Standard } - Notes:
I didn't have to change this at all. The original programmer was kind to leave a comment for how this is really supposed to work. There really should be two menu items to mirror windows calculator: one for the standard view and another for the scientific view. To keep things simple, there's no scientific view so there's no other menu item. For now, clicking this menu item keeps everything the same but it's clear how to modify it to add a scientific view, and how to manipulate other menu items.
- C#:
I should mention that I declared "StandardMenu" outside all the methods so that it could be used by both the "InitializeThis" (for set-up) and "OnMenuStandard" (for action) methods.
Both versions now look and act exactly the same way (they both have the same bugs consequently). The Alarm Clock Sample port I wrote ended up having a smaller executable than the original but, for this calculator, the port is larger. That's interesting to me. I wonder where the difference lies.
Quite an informative post this gives a deep insight for a beginner of C# and will provide that kick start needed to keep one going.
ReplyDelete