I am fairly new to C#, and this is my first attempt at creating a windows form. I created a very simple calculator which has two textboxes for users to enter numbers into. It has a(n) Add, Subtract, Multiply, and Divide button, and when the user enters values into the textboxes and clicks one of the buttons, the result is shown in a label. I would like to only allow integers to be input into the textboxes, but I don't know how I would be able to do this. Any advice or recommendations are appreciated. Thanks.
Here is my code so far:
namespace SimpleCalc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void AddBtn_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
ResultLbl.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text)).ToString();
}
private void SubtractBtn_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
ResultLbl.Text = (Convert.ToInt32(textBox1.Text) - Convert.ToInt32(textBox2.Text)).ToString();
}
private void MultiplyBtn_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
ResultLbl.Text = (Convert.ToInt32(textBox1.Text) * Convert.ToInt32(textBox2.Text)).ToString();
}
private void DivideBtn_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
ResultLbl.Text = (Convert.ToInt32(textBox1.Text) / Convert.ToInt32(textBox2.Text)).ToString();
}
}
}