Advertisement
ayurchyk1998

Untitled

Nov 3rd, 2020
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. using System.ComponentModel;
  8. using System.Drawing;
  9. using System.Windows.Forms;
  10. using System.Runtime.CompilerServices;
  11. using System.Runtime.InteropServices;
  12.  
  13. namespace Calculator_lab1_
  14. {
  15.     class Calc : Form
  16.     {
  17.         #region Declare buttons
  18.         public static Button butPlus;
  19.         public static Button butMinus;
  20.         public static Button butMultiply;
  21.         public static Button butDivide;
  22.         public static Button butRemove;
  23.         #endregion
  24.  
  25.         #region Declare textboxes
  26.         public static TextBox firstNum;
  27.         public static TextBox secondNum;
  28.         public static TextBox result;
  29.         #endregion
  30.  
  31.         #region Declare numbs
  32.         private float fnum;
  33.         private float snum;
  34.         private float res;
  35.         #endregion
  36.  
  37.         public string mess = null;
  38.  
  39.         #region Declare labels
  40.         private static Label symbol;
  41.         private static Label equals;
  42.         #endregion
  43.  
  44.         #region Close Console
  45.         const int SW_HIDE = 0;
  46.         /*const int SW_SHOW = 5;
  47.         const int SW_Min = 2;
  48.         const int SW_Max = 3;
  49.         const int SW_Norm = 4;*/
  50.  
  51.         [DllImport("kernel32.dll")]
  52.         static extern IntPtr GetConsoleWindow();
  53.  
  54.         [DllImport("user32.dll")]
  55.         static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  56.         #endregion
  57.         static void Main(string[] args)
  58.         {
  59.             Application.EnableVisualStyles();
  60.             Application.SetCompatibleTextRenderingDefault(false);
  61.             Application.Run(new Calc());
  62.            
  63.             //отобразить консоль
  64.             //ShowWindow(handle, SW_SHOW);
  65.  
  66.             //свернуть консоль
  67.             //ShowWindow(handle, SW_Min);
  68.  
  69.             //развернуть консоль
  70.             //ShowWindow(handle, SW_Max);
  71.  
  72.             //нормальный размер консоли
  73.             //ShowWindow(handle, SW_Norm);
  74.         }
  75.  
  76.         public Calc()
  77.         {
  78.             var handle = GetConsoleWindow();
  79.             //скрыть консоль
  80.             ShowWindow(handle, SW_HIDE);
  81.  
  82.             #region Buttons
  83.             butPlus = new Button();
  84.             butPlus.Size = new Size(50, 30);
  85.             butPlus.Location = new Point(10, 80);
  86.             butPlus.Text = "Plus";
  87.             this.Controls.Add(butPlus);
  88.             butPlus.Click += new EventHandler(butPlus_Click);
  89.  
  90.             butMinus = new Button();
  91.             butMinus.Size = new Size(55, 30);
  92.             butMinus.Location = new Point(70, 80);
  93.             butMinus.Text = "Minus";
  94.             this.Controls.Add(butMinus);
  95.             butMinus.Click += new EventHandler(butMinus_Click);
  96.  
  97.             butMultiply = new Button();
  98.             butMultiply.Size = new Size(65, 30);
  99.             butMultiply.Location = new Point(135, 80);
  100.             butMultiply.Text = "Multiply";
  101.             this.Controls.Add(butMultiply);
  102.             butMultiply.Click += new EventHandler(butMultiply_Click);
  103.  
  104.             butDivide = new Button();
  105.             butDivide.Size = new Size(55, 30);
  106.             butDivide.Location = new Point(210, 80);
  107.             butDivide.Text = "Divide";
  108.             this.Controls.Add(butDivide);
  109.             butDivide.Click += new EventHandler(butDivide_Click);
  110.  
  111.             butRemove = new Button();
  112.             butRemove.Size = new Size(38, 30);
  113.             butRemove.Location = new Point(5, 210);
  114.             butRemove.Text = "Del";
  115.             this.Controls.Add(butRemove);
  116.             butRemove.Click += new EventHandler(butRemove_Click);
  117.             #endregion
  118.  
  119.             #region TextBoxes
  120.             firstNum = new TextBox();
  121.             firstNum.Size = new Size(78, 30);
  122.             firstNum.Location = new Point(5, 30);
  123.             this.Controls.Add(firstNum);
  124.  
  125.             secondNum = new TextBox();
  126.             secondNum.Size = new Size(78, 30);
  127.             secondNum.Location = new Point(98, 30);
  128.             this.Controls.Add(secondNum);
  129.  
  130.             result = new TextBox();
  131.             result.Size = new Size(79, 30);
  132.             result.Location = new Point(191, 30);
  133.             this.Controls.Add(result);
  134.             #endregion
  135.  
  136.             symbol = new Label();
  137.             symbol.AutoSize = true;
  138.             symbol.Location = new Point(84, 31);
  139.             symbol.ForeColor = System.Drawing.Color.Red;
  140.             symbol.Text = null;
  141.             this.Controls.Add(symbol);
  142.  
  143.             equals = new Label();
  144.             equals.AutoSize = true;
  145.             equals.Location = new Point(176, 31);
  146.             equals.ForeColor = System.Drawing.Color.Red;
  147.             equals.Text = "=";
  148.             this.Controls.Add(equals);
  149.  
  150.  
  151.         } // size 265
  152.  
  153.         #region ClickPlus
  154.         private void butPlus_Click(object sender, EventArgs e)
  155.         {
  156.             try
  157.             {
  158.                 fnum = float.Parse(firstNum.Text);
  159.                 snum = float.Parse(secondNum.Text);
  160.             }
  161.             catch
  162.             {
  163.                 mess = "Wrong sign found";
  164.                 MessageBox.Show("Wrong sign found");
  165.             }
  166.             if (!(mess == "Wrong sign found"))
  167.             {
  168.                 res = fnum + snum;
  169.                 result.Text = res.ToString();
  170.                 symbol.Text = "+";
  171.             }
  172.             mess = null;
  173.         }
  174.         #endregion
  175.         #region ClickMinus
  176.         private void butMinus_Click(object sender, EventArgs e)
  177.         {
  178.             try
  179.             {
  180.                 fnum = float.Parse(firstNum.Text);
  181.                 snum = float.Parse(secondNum.Text);
  182.             }
  183.             catch
  184.             {
  185.                 mess = "Wrong sign found";
  186.                 MessageBox.Show("Wrong sign found");
  187.             }
  188.             if (!(mess == "Wrong sign found"))
  189.             {
  190.                 res = fnum - snum;
  191.                 result.Text = res.ToString();
  192.                 symbol.Text = "-";
  193.             }
  194.             mess = null;
  195.         }
  196.         #endregion
  197.         #region ClickMultiply
  198.         private void butMultiply_Click(object sender, EventArgs e)
  199.         {
  200.             try
  201.             {
  202.                 fnum = float.Parse(firstNum.Text);
  203.                 snum = float.Parse(secondNum.Text);
  204.             }
  205.             catch
  206.             {
  207.                 mess = "Wrong sign found";
  208.                 MessageBox.Show("Wrong sign found");
  209.             }
  210.             if (!(mess == "Wrong sign found"))
  211.             {
  212.                 res = fnum * snum;
  213.                 result.Text = res.ToString();
  214.                 symbol.Text = "*";
  215.             }
  216.             mess = null;
  217.         }
  218.         #endregion
  219.         #region ClickDivide
  220.         private void butDivide_Click(object sender, EventArgs e)
  221.         {
  222.             try
  223.             {
  224.                 fnum = float.Parse(firstNum.Text);
  225.                 snum = float.Parse(secondNum.Text);
  226.             }
  227.             catch
  228.             {
  229.                 mess = "Wrong sign found";
  230.                 MessageBox.Show("Wrong sign found");
  231.             }
  232.             if (!(mess == "Wrong sign found"))
  233.             {
  234.                 res = fnum / snum;
  235.                 result.Text = res.ToString();
  236.                 symbol.Text = "/";
  237.             }
  238.             mess = null;
  239.         }
  240.         #endregion
  241.         #region ClickRemove
  242.         private void butRemove_Click(object sender, EventArgs e)
  243.         {
  244.             firstNum.Clear();
  245.             secondNum.Clear();
  246.             result.Clear();
  247.             symbol.Text = "";
  248.             equals.Text = "";
  249.         }
  250.         #endregion
  251.     }
  252. }
  253.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement