I created a simple calculator to perform addition subraction multiplication and division. the problem in the code below is repeated code to get text from textField and parsing it into the int everytime a operation is performed. And I know there's way to do it in less lines of code so help me here.
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 formApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void nameLbl_Click(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Submit_Click(object sender, EventArgs e)
{
}
private void label1_Click_1(object sender, EventArgs e)
{
}
private void addBtn_Click(object sender, EventArgs e)
{
int num1 = 0;
int num2 = 0;
String num = firstNum.Text;
String sNum = secondNum.Text;
if (num == "")
{
MessageBox.Show("error type both num");
}
else
{
num1 = int.Parse(num);
num2 = int.Parse(sNum);
}
int total = num1 + num2;
String sum = (total).ToString();
MessageBox.Show(sum);
}
private void firstNum_TextChanged(object sender, EventArgs e)
{
}
private void multBtn_Click(object sender, EventArgs e)
{
int num1 = 0;
int num2 = 0;
String num = firstNum.Text;
String sNum = secondNum.Text;
if (num == "")
{
MessageBox.Show("error type both num");
}
else
{
num1 = int.Parse(num);
num2 = int.Parse(sNum);
}
int total = num1 * num2;
MessageBox.Show(total.ToString());
}
private void subBtn_Click(object sender, EventArgs e)
{
int num1 = 0;
int num2 = 0;
String num = firstNum.Text;
String sNum = secondNum.Text;
if (num == "")
{
MessageBox.Show("error type both num");
}
else
{
num1 = int.Parse(num);
num2 = int.Parse(sNum);
}
int total = num1 - num2;
MessageBox.Show(total.ToString());
}
private void divideBtn_Click(object sender, EventArgs e)
{
int num1 = 0;
int num2 = 0;
String num = firstNum.Text;
String sNum = secondNum.Text;
if (num == "")
{
MessageBox.Show("error type both num");
}
else
{
num1 = int.Parse(num);
num2 = int.Parse(sNum);
}
int total = num1 / num2;
MessageBox.Show(total.ToString());
firstNum.Text = string.Empty;
secondNum.Text = string.Empty;
}
}
}