nuggetin

act

Jan 21st, 2022 (edited)
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3. using CalculatorPrivateAssembly;
  4.  
  5. namespace BasicCalculator1
  6. {
  7.     public partial class frmBasicCalculator : Form
  8.     {
  9.         public frmBasicCalculator()
  10.         {
  11.             InitializeComponent();
  12.         }
  13.  
  14.         private void button1_Click(object sender, EventArgs e)
  15.         {
  16.             if (cbOperator.SelectedItem == null)
  17.             {
  18.                 rtbResult.Text = "Error:\n" + "Please select an Operator.";
  19.             }
  20.             float a = 0, b = 0;
  21.             parseCheck(tbNum1, out a, "First");
  22.             parseCheck(tbNum2, out b, "Second");
  23.  
  24.             if (a != 0 && b != 0 && cbOperator != null)
  25.             {
  26.                 switch (cbOperator.SelectedItem)
  27.                 {
  28.                     case "x":
  29.                         printResult(BasicCalculator.Multiplication(a, b));
  30.                         break;
  31.                     case "/":
  32.                         printResult(BasicCalculator.Division(a, b));
  33.                         break;
  34.                     case "+":
  35.                         printResult(BasicCalculator.Addition(a, b));
  36.                         break;
  37.                     case "-":
  38.                         printResult(BasicCalculator.Subtraction(a, b));
  39.                         break;
  40.                 }
  41.             }
  42.         }
  43.  
  44.         void parseCheck(TextBox a, out float b, string c)
  45.         {
  46.             if (!float.TryParse(a.Text, out b))
  47.             {
  48.                 rtbResult.Text = "Error:\n" + c + " entry is invalid";
  49.             }
  50.         }
  51.  
  52.         void printResult(float a)
  53.         {
  54.             rtbResult.Text = "Result:\n" + a.ToString();
  55.         }
  56.     }
  57. }
  58.  
Add Comment
Please, Sign In to add comment