0% found this document useful (0 votes)
50 views

Calcula Dora Basic A

This document contains code for a basic calculator application in C#. It includes code for a Form1 class that initializes the form and handles button clicks to perform arithmetic operations. It also includes a ComprovarCampo class that checks textbox input is within an ASCII range, likely to validate numeric input. The code allows the user to select an operation, enter two numbers, click calculate to perform the operation and display the result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Calcula Dora Basic A

This document contains code for a basic calculator application in C#. It includes code for a Form1 class that initializes the form and handles button clicks to perform arithmetic operations. It also includes a ComprovarCampo class that checks textbox input is within an ASCII range, likely to validate numeric input. The code allows the user to select an operation, enter two numbers, click calculate to perform the operation and display the result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Aleix Collado Serrano

FORM1.CS
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 CalculadoraBasica
{
public partial class Form1 : Form

{
int operation = 0;
public Form1()
{
InitializeComponent();
}

private void label1_Click(object sender, EventArgs e)


{

private void label3_Click(object sender, EventArgs e)


{

private void textBox3_TextChanged(object sender, EventArgs e)


{

private void Form1_Load(object sender, EventArgs e)


{

private void button4_Click(object sender, EventArgs e)


{
Loperation.Text = "X";
operation = 1;
}

private void button2_Click(object sender, EventArgs e)


{
Loperation.Text = "/";
operation = 2;
}

private void button3_Click(object sender, EventArgs e)


{
Aleix Collado Serrano

Loperation.Text = "+";
operation = 3;
}

private void button1_Click(object sender, EventArgs e)


{
Loperation.Text = "-";
operation = 4;
}
private void button6_Click(object sender, EventArgs e)
{
Loperation.Text = "MOD";
operation = 5;
}
private void button5_Click(object sender, EventArgs e)
{
switch (operation)

case 1:
tresultado.Text = Convert.ToString(int.Parse(tnombre1.Text) *
int.Parse(tnombre2.Text));
break;
case 2:
tresultado.Text = Convert.ToString(int.Parse(tnombre1.Text) /
int.Parse(tnombre2.Text));
break;
case 3:
tresultado.Text = Convert.ToString(int.Parse(tnombre1.Text) +
int.Parse(tnombre2.Text));
break;
case 4:
tresultado.Text = Convert.ToString(int.Parse(tnombre1.Text) -
int.Parse(tnombre2.Text));
break;
case 5:
tresultado.Text = Convert.ToString(int.Parse(tnombre1.Text) %
int.Parse(tnombre2.Text));
break;
default:
MessageBox.Show("Ha de seleccionar una operació abans.");
break;

private void tnombre1_TextChanged(object sender, EventArgs e)


{

ComprovarCampo.check((System.Windows.Forms.TextBox)sender, 48, 57);


}

private void tnombre2_TextChanged(object sender, EventArgs e)


{
Aleix Collado Serrano

ComprovarCampo.check((System.Windows.Forms.TextBox)sender, 48, 57);

}
}

ComprovarCampo.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CalculadoraBasica
{
class ComprovarCampo
{

public static Boolean check(TextBox textbx, int asciiminvalue, int


asciimaxvalue)
{
Boolean res = false;
TextBox textbox = textbx;
int tmp = textbox.SelectionStart - 1;
byte[] ascii = Encoding.ASCII.GetBytes(textbox.Text);
for (int i = 0; i < ascii.Length; i++)
{
if (ascii[i] < asciiminvalue || ascii[i] > asciimaxvalue)
{
textbox.Text = textbox.Text.Remove(tmp, 1);
textbox.SelectionStart = tmp;
res = true;
}

}
return res;
}
}
}
Aleix Collado Serrano

Capturas de pantalla

You might also like