Advertisement
Guest User

Calculadora

a guest
Feb 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace Calculadora
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         double num1, num2,resp;
  16.  
  17.         private void button2_Click(object sender, EventArgs e)
  18.         {
  19.             if (textBox1.Text == "" || textBox2.Text == "")
  20.             {
  21.                 MessageBox.Show("Preencha todos os campos corretamente!");
  22.                 return;
  23.             }
  24.  
  25.             num1 = double.Parse(textBox1.Text);
  26.             num2 = double.Parse(textBox2.Text);
  27.             resp = num1 - num2;
  28.             label4.Text = resp.ToString();
  29.         }
  30.  
  31.         private void button3_Click(object sender, EventArgs e)
  32.         {
  33.             if (textBox1.Text == "" || textBox2.Text == "")
  34.             {
  35.                 MessageBox.Show("Preencha todos os campos corretamente!");
  36.                 return;
  37.             }
  38.  
  39.             num1 = double.Parse(textBox1.Text);
  40.             num2 = double.Parse(textBox2.Text);
  41.             resp = num1 * num2;
  42.             label4.Text = resp.ToString();
  43.         }
  44.  
  45.         private void button4_Click(object sender, EventArgs e)
  46.         {
  47.             if (textBox1.Text == "" || textBox2.Text == "")
  48.             {
  49.                 MessageBox.Show("Preencha todos os campos corretamente!");
  50.                 return;
  51.             }
  52.  
  53.             num1 = double.Parse(textBox1.Text);
  54.             num2 = double.Parse(textBox2.Text);
  55.  
  56.             if(num2 == 0.0)
  57.             {
  58.                 label4.Text = "Impossível dividir por zero";
  59.                 return;
  60.             }
  61.  
  62.             resp = num1 / num2;
  63.             label4.Text = resp.ToString();
  64.         }
  65.  
  66.         private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  67.         {
  68.             //esses dois ifs fazem a verificação do que o usuário está digitando
  69.             //caso ele digite algo diferente de número e vírgula, o sistema o informará
  70.             //e jogará no lixo qualquer valor anormal para não dar erro na conversão de texto para double
  71.             if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != ','))
  72.             {
  73.                 e.Handled = true;
  74.                 MessageBox.Show("este campo aceita somente numero e virgula");
  75.             }
  76.             if ((e.KeyChar == ',') && ((sender as TextBox).Text.IndexOf('.') > -1))
  77.             {
  78.                 e.Handled = true;
  79.                 MessageBox.Show("este campo aceita somente uma virgula");
  80.             }
  81.         }
  82.  
  83.         private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
  84.         {
  85.             //esses dois ifs fazem a verificação do que o usuário está digitando
  86.             //caso ele digite algo diferente de número e vírgula, o sistema o informará
  87.             //e jogará no lixo qualquer valor anormal para não dar erro na conversão de texto para double
  88.             if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != ','))
  89.             {
  90.                 e.Handled = true;
  91.                 MessageBox.Show("este campo aceita somente numero e virgula");
  92.             }
  93.             if ((e.KeyChar == ',') && ((sender as TextBox).Text.IndexOf('.') > -1))
  94.             {
  95.                 e.Handled = true;
  96.                 MessageBox.Show("este campo aceita somente uma virgula");
  97.             }
  98.         }
  99.  
  100.         public Form1()
  101.         {
  102.             InitializeComponent();
  103.         }
  104.  
  105.         private void button1_Click(object sender, EventArgs e)
  106.         {
  107.             if(textBox1.Text == "" || textBox2.Text == "")
  108.             {
  109.                 MessageBox.Show("Preencha todos os campos corretamente!");
  110.                 return;
  111.             }
  112.  
  113.             num1 = double.Parse(textBox1.Text);
  114.             num2 = double.Parse(textBox2.Text);
  115.             resp = num1 + num2;
  116.             label4.Text = resp.ToString();
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement