/// <summary> /// OnFractionToggle() - Toggles display between fractional and floating point affecting the /// display fields of input and result. Displays either the "/" key or the "." depending /// whether using fractions or decimals. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// Side Effects: /// Changes GLOBALS Fraction_class instance InBound and its display string sbInBound. /// Changes GLOBALS Fraction_class instance Accumulator and display string sbAccumulator. /// private void OnFractionToggle(object sender, EventArgs e) { string workString = this.FindByName <Button>("DotSlash").Text; workString = workString == "/" ? "." : "/"; this.FindByName <Button>("DotSlash").Text = workString; if (workString == ".") { displayToggle = DisplayToggle.floating; this.FindByName <Button>("FractionToggle").Text = "Fraction"; this.FindByName <Button>("SpaceBar").IsVisible = false; InBound.Parse(this.FindByName <Label>("Input").Text); sbInBound.Clear(); sbInBound.Append(InBound.ToDisplayFloat(" ")); this.FindByName <Label>("Input").Text = sbInBound.ToString(); this.FindByName <Label>("Input").FontSize = shrinkLabel(this.FindByName <Label>("Input"), 8, 60, sbInBound.ToString()); sbAccumulator.Clear(); sbAccumulator.Append(Accumulator.ToDisplayFloat(" ")); Accumulator.Parse(sbAccumulator.ToString()); this.FindByName <Label>("Result").Text = sbAccumulator.ToString(); this.FindByName <Label>("Result").FontSize = shrinkLabel(this.FindByName <Label>("Result"), 16, 30, sbInBound.ToString()); } else { displayToggle = DisplayToggle.fractional; this.FindByName <Button>("FractionToggle").Text = "Decimal"; this.FindByName <Button>("SpaceBar").IsVisible = true; sbInBound.Clear(); sbInBound.Append(InBound.ToDisplayFraction(" ")); InBound.Parse(sbInBound.ToString()); this.FindByName <Label>("Input").Text = sbInBound.ToString(); this.FindByName <Label>("Input").FontSize = shrinkLabel(this.FindByName <Label>("Input"), 8, 60, sbInBound.ToString()); sbAccumulator.Clear(); // whats the best way to set stringbuilder? sbAccumulator.Append(Accumulator.ToDisplayFraction(" ")); Accumulator.Parse(sbAccumulator.ToString()); this.FindByName <Label>("Result").Text = sbAccumulator.ToString(); this.FindByName <Label>("Result").FontSize = shrinkLabel(this.FindByName <Label>("Result"), 16, 30, sbInBound.ToString()); } }
// try/catch? private void OnOperatorPressed(object sender, EventArgs e) { // procedurally there's no input yet, maybe better way to do this if (this.FindByName <Label>("Input").Text is null || this.FindByName <Label>("Input").Text.Trim().Length == 0) { lastOperator = (sender as Button).Text.ToCharArray()[0]; return; } // if (InBound.Parse(sbInBound.ToString()) == false) { this.FindByName <Label>("Input").Text = String.Empty; // nasty error response. sbInBound.Clear(); return; } sbInBound.Clear(); this.FindByName <Label>("Input").Text = sbInBound.ToString(); UndoAccum.SetValueTo(Accumulator); switch (lastOperator) { case '+': Accumulator.Add(InBound); break; case '-': Accumulator.Subtract(InBound); break; case '/': Accumulator.DivideBy(InBound); break; case '*': Accumulator.MultiplyBy(InBound); break; case '=': if (Accumulator.ToDisplayFloat("0") == "0") { Accumulator.SetValueTo(InBound); } break; case '%': Accumulator.Modulus(InBound); break; default: //-- Accumulator.SetValueTo(InBound); break; } ; lastOperator = (sender as Button).Text.ToCharArray()[0]; sbAccumulator.Clear(); // whats the best way to set stringbuilder? if (displayToggle == DisplayToggle.fractional) { sbAccumulator.Append(Accumulator.ToDisplayFraction()); } else { sbAccumulator.Append(Accumulator.ToDisplayFloat()); } this.FindByName <Label>("Result").Text = sbAccumulator.ToString(); this.FindByName <Label>("Result").FontSize = shrinkLabel(this.FindByName <Label>("Result"), 16, 30, sbAccumulator.ToString()); }