The last thing I wanted to do was complete the calculator's memory. I saved it for last because I thought it would be very simple after finishing everything else. [Unfortunatley, I lied. The memory won't be the last thing to do. I forgot about the menu, but that should be even easier.] I noticed that the original code used a class called "PaperTrail" which is the last class defined in window1.xaml.cs.
- PaperTrail
- C#:
private class PaperTrail { string args; public PaperTrail() { } public void AddArguments(string a) { args = a; } public void AddResult(string r) { PaperBox.Text += args + " = " + r + "\n"; } public void Clear() { PaperBox.Text = string.Empty; args = string.Empty; } } - Notes:
This is a very simple class. The constructor takes no arguments and does nothing. The only three methods are very simple in terms of how complex the code is but they don't do what I expected them to do. I thought something described as a "paper trail" would keep a record of the trail but it does no such thing. It calls upon "PaperBox" to keep the record.
- C#:
- PaperBox
- C#:
static MyTextBox PaperBox; ... PaperBox = new MyTextBox(); Grid.SetRow(PaperBox, 1); Grid.SetColumn(PaperBox, 0); Grid.SetColumnSpan(PaperBox, 3); Grid.SetRowSpan(PaperBox, 5); PaperBox.IsReadOnly = true; PaperBox.VerticalScrollBarVisibility = ScrollBarVisibility.Visible; PaperBox.Margin = new Thickness(3.0,1.0,1.0,1.0); PaperBox.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto; - Notes:
You can find "PaperBox" defined at the top of the "Window1" class definition. It is simply a textbox like "DisplayBox" but subclassed differently in the "Window1" constructor to have a different size using the "SetColumnSpan" and "SetRowSpan" methods, different interaction by setting the "IsReadOnly" property to "true", and a different visual style by making the scroll bars visible, and setting up the margins.
- C#:
Now that there's a place to store memory information, there need to be appropriate buttons to interact with the memory. Microsoft's programmer decided to have Memory Clear, Memory Save, Memory Recall, and Memory Plus operations. The are found in the "ProcessOperation" method //Comments by original programmer:
- Memory Clear
- C#:
case "BMemClear": Memory = 0.0F; DisplayMemory(); break; - Notes:
The memory value is stored as a string "_mem_val" accessed through the property "Memory". Here we want to clear the memory, so the mathematical thing to do is set it to 0 (floating point of course). All of the operations which alter memory call the "DisplayMemory" method.
- C#:
- Memory Save
- C#:
case "BMemSave": Memory = Convert.ToDouble(Display); DisplayMemory(); EraseDisplay = true; break; - Notes:
Because of
EraseDisplay = true;, if you save the display to memory when you are in the middle of an operation, you need to key-in another value (maybe the same one) if you want to perform a binary operation. That is, the number is saved to memory and continues to be displayed but any binary operation will not see it. Unary operations just take the value from the display so they will work as expected.
- C#:
- Memory Recall
- C#:
case "BMemRecall": Display = /*val =*/ Memory.ToString(); UpdateDisplay(); //if (LastOper != Operation.None) //using MR is like entring a digit EraseDisplay = false; break; - Notes:
Because of "EraseDisplay = false;", memory recall is like entering a digit. I guess the commented-out partial conditional would have been to make MR act like completing a binary operation with the saved value.
- C#:
- Memory Plus
- C#:
case "BMemPlus": d = Memory + Convert.ToDouble(Display); Memory = d; DisplayMemory(); EraseDisplay = true; break; - Notes:
The value in the display is added to the value in memory and then saved in memory. Like "BMemSave", this effectively erases the display so binary operations won't see it.
- C#:
The DisplayMemory method is a bit naughty. It accesses "_mem_val" directly instead of through the "Memory" property. The author went through pains to start the variable name with an underscore so you would expect him to follow the practice of pretending he can't access it without the approperiate getter.... Anyway, it also uses a certain object called "BMemBox" which is defined in XAML so I had to port it to C#:
- Memory Display Box
- XAML:
<textblock name="BMemBox" grid.column="3" grid.row="1" margin="10,17,10,17" grid.columnspan="2">Memory: [empty]</textblock> - C#:
BMemBox = new TextBlock(); BMemBox.Name = "BMemBox"; Grid.SetColumn(BMemBox, 3); Grid.SetRow(BMemBox, 1); BMemBox.Margin = new Thickness(10.0, 17.0, 10.0, 17.0); Grid.SetColumnSpan(BMemBox, 2); BMemBox.Text = "Memory: [empty]"; MyGrid.Children.Add(BMemBox); - Notes:
This is very similar to rendering the buttons but there's no click event. The styles are all default too.
- XAML:
I thought this would be the last part to the Calculator Demo series but I forgot about the menu. The next part should be the last part then I will get to choose another program to analyse.
No comments:
Post a Comment