I am learning to code in c#, so if I do not explain everything ok, do not kill me! I an practising with c#, trying to do a very simple calculator(only add), but when I write a letter(not number), the program crash because the textbox is saving data in a Int. I ask how to prohibit writting letters, but no numbers, so it doesn´t crash. Thanks! Sorry for not commenting the code, i am still forgetting doing that! Here is the relevant code:
using System;
using System.Windows.Forms;
namespace calculadora
{
public partial class Suma : Form
{
public Suma()
{
InitializeComponent();
}
private void Suma_Load(object sender, EventArgs e)
{
}
public int resultadoint, op1, op2; //declaracion de variables.
public string resultado; //declaracion de variable a mostrar.
private void button1_Click(object sender, EventArgs e)
{
resultadoint = op1 + op2;
resultadotext.Text = resultadoint.ToString();
}
private void label2_Click(object sender, EventArgs e)
{
//solo indicador de suma
}
private void label4_Click(object sender, EventArgs e)
{
}
private void operador1_TextChanged(object sender, EventArgs e) //se intruduce aquí el primer operador, op1.
{
op1 = int.Parse(operador1.Text);
}
private void operador2_TextChanged(object sender, EventArgs e) //se intruduce aquí el segundo operador, op2.
{
op2 = int.Parse(operador2.Text);
}
}
My code do Not use KeyPress.