Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Calculadora
- {
- public partial class Form1 : Form
- {
- double num1, num2,resp;
- private void button2_Click(object sender, EventArgs e)
- {
- if (textBox1.Text == "" || textBox2.Text == "")
- {
- MessageBox.Show("Preencha todos os campos corretamente!");
- return;
- }
- num1 = double.Parse(textBox1.Text);
- num2 = double.Parse(textBox2.Text);
- resp = num1 - num2;
- label4.Text = resp.ToString();
- }
- private void button3_Click(object sender, EventArgs e)
- {
- if (textBox1.Text == "" || textBox2.Text == "")
- {
- MessageBox.Show("Preencha todos os campos corretamente!");
- return;
- }
- num1 = double.Parse(textBox1.Text);
- num2 = double.Parse(textBox2.Text);
- resp = num1 * num2;
- label4.Text = resp.ToString();
- }
- private void button4_Click(object sender, EventArgs e)
- {
- if (textBox1.Text == "" || textBox2.Text == "")
- {
- MessageBox.Show("Preencha todos os campos corretamente!");
- return;
- }
- num1 = double.Parse(textBox1.Text);
- num2 = double.Parse(textBox2.Text);
- if(num2 == 0.0)
- {
- label4.Text = "Impossível dividir por zero";
- return;
- }
- resp = num1 / num2;
- label4.Text = resp.ToString();
- }
- private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
- {
- //esses dois ifs fazem a verificação do que o usuário está digitando
- //caso ele digite algo diferente de número e vírgula, o sistema o informará
- //e jogará no lixo qualquer valor anormal para não dar erro na conversão de texto para double
- if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != ','))
- {
- e.Handled = true;
- MessageBox.Show("este campo aceita somente numero e virgula");
- }
- if ((e.KeyChar == ',') && ((sender as TextBox).Text.IndexOf('.') > -1))
- {
- e.Handled = true;
- MessageBox.Show("este campo aceita somente uma virgula");
- }
- }
- private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
- {
- //esses dois ifs fazem a verificação do que o usuário está digitando
- //caso ele digite algo diferente de número e vírgula, o sistema o informará
- //e jogará no lixo qualquer valor anormal para não dar erro na conversão de texto para double
- if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != ','))
- {
- e.Handled = true;
- MessageBox.Show("este campo aceita somente numero e virgula");
- }
- if ((e.KeyChar == ',') && ((sender as TextBox).Text.IndexOf('.') > -1))
- {
- e.Handled = true;
- MessageBox.Show("este campo aceita somente uma virgula");
- }
- }
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- if(textBox1.Text == "" || textBox2.Text == "")
- {
- MessageBox.Show("Preencha todos os campos corretamente!");
- return;
- }
- num1 = double.Parse(textBox1.Text);
- num2 = double.Parse(textBox2.Text);
- resp = num1 + num2;
- label4.Text = resp.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement