form1(calculator)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

using System.Data.

SqlClient;

namespace CalculatorForm
{
public partial class Form1 : Form
{
SqlConnection con;
Double resultValue = 0;
String operationPerformed = "";
bool isOperationPerformed = false;
public Form1()
{
InitializeComponent();
con = new SqlConnection("Data Source=Adils-PC;Initial
Catalog=calculator;Integrated Security=True;");
con.Open();
}

private void button9_Click(object sender, EventArgs e)


{
if (txtBoxResult.Text == "0")
{
txtBoxResult.Clear();
}
//txtBoxResult.Text = txtBoxResult.Text + "*";
Button button = (Button)sender;
operationPerformed = button.Text;
resultValue = Double.Parse(txtBoxResult.Text);
label1.Text = resultValue.ToString() + "" + operationPerformed;
isOperationPerformed = true;
}

private void button16_Click(object sender, EventArgs e)


{
if (txtBoxResult.Text == "0" || (isOperationPerformed))
{
txtBoxResult.Clear();
}
txtBoxResult.Text = txtBoxResult.Text + "1";
isOperationPerformed = false;
}

private void button15_Click(object sender, EventArgs e)


{
if (txtBoxResult.Text == "0" || (isOperationPerformed))
{
txtBoxResult.Clear();
}
txtBoxResult.Text = txtBoxResult.Text + "2";
isOperationPerformed = false;
}

private void button10_Click(object sender, EventArgs e)


{
if (txtBoxResult.Text == "0" || (isOperationPerformed))
{
txtBoxResult.Clear();
}
txtBoxResult.Text = txtBoxResult.Text + "3";
isOperationPerformed = false;
}

private void button8_Click(object sender, EventArgs e)


{
if (txtBoxResult.Text == "0" || (isOperationPerformed))
{
txtBoxResult.Clear();
}
txtBoxResult.Text = txtBoxResult.Text + "4";
isOperationPerformed = false;
}

private void button6_Click(object sender, EventArgs e)


{
if (txtBoxResult.Text == "0")
{
txtBoxResult.Clear();
}
txtBoxResult.Text = txtBoxResult.Text + "5";
isOperationPerformed = false;
}

private void button7_Click(object sender, EventArgs e)


{
if (txtBoxResult.Text == "0" || (isOperationPerformed))
{
txtBoxResult.Clear();
}
txtBoxResult.Text = txtBoxResult.Text + "6";
isOperationPerformed = false;
}

private void button1_Click(object sender, EventArgs e)


{
if (txtBoxResult.Text == "0" || (isOperationPerformed))
{
txtBoxResult.Clear();
}
txtBoxResult.Text = txtBoxResult.Text + "7";
isOperationPerformed = false;
}

private void button2_Click(object sender, EventArgs e)


{
if (txtBoxResult.Text == "0")
{
txtBoxResult.Clear();
}
txtBoxResult.Text = txtBoxResult.Text + "8";
isOperationPerformed = false;
}

private void button3_Click(object sender, EventArgs e)


{
if (txtBoxResult.Text == "0" || (isOperationPerformed))
{
txtBoxResult.Clear();
}
txtBoxResult.Text = txtBoxResult.Text + "9";
isOperationPerformed = false;
}

private void button20_Click(object sender, EventArgs e)


{
if (txtBoxResult.Text == "0")
{
txtBoxResult.Clear();
}
txtBoxResult.Text = txtBoxResult.Text + ".";
}

private void button19_Click(object sender, EventArgs e)


{
if (txtBoxResult.Text == "0")
{
txtBoxResult.Clear();
}
//txtBoxResult.Text = txtBoxResult.Text + "+";
Button button = (Button)sender;
operationPerformed = button.Text;
resultValue = Double.Parse(txtBoxResult.Text);
label1.Text = resultValue.ToString() + "" + operationPerformed;
isOperationPerformed = true;
}

private void button13_Click(object sender, EventArgs e)


{
if (txtBoxResult.Text == "0")
{
txtBoxResult.Clear();
}
//txtBoxResult.Text = txtBoxResult.Text + "-";
Button button = (Button)sender;
operationPerformed = button.Text;
resultValue = Double.Parse(txtBoxResult.Text);
label1.Text = resultValue.ToString() + "" + operationPerformed;
isOperationPerformed = true;
}

private void button4_Click(object sender, EventArgs e)


{
if (txtBoxResult.Text == "0")
{
txtBoxResult.Clear();
}
//txtBoxResult.Text = txtBoxResult.Text + "/";
Button button = (Button)sender;
operationPerformed = button.Text;
resultValue = Double.Parse(txtBoxResult.Text);
label1.Text = resultValue.ToString() + "" + operationPerformed;
isOperationPerformed = true;
}
private void button5_Click(object sender, EventArgs e)
{
if (txtBoxResult.Text == "0")
{
txtBoxResult.Clear();
}
txtBoxResult.Text = "0";
}

private void button12_Click(object sender, EventArgs e)


{
txtBoxResult.Text = txtBoxResult.Text + "0";
}

private void button14_Click(object sender, EventArgs e)


{
string insertQuery = "INSERT INTO historys VALUES (@results)";
SqlCommand cmd = new SqlCommand(insertQuery, con);
switch (operationPerformed)
{
case "+":
txtBoxResult.Text = (resultValue +
Double.Parse(txtBoxResult.Text)).ToString();
cmd.Parameters.AddWithValue("@results", txtBoxResult.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Data Saved Successfully", "info",
MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case "-":
txtBoxResult.Text = (resultValue -
Double.Parse(txtBoxResult.Text)).ToString();
cmd.Parameters.AddWithValue("@results", txtBoxResult.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Data Saved Successfully", "info",
MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case "/":
txtBoxResult.Text = (resultValue /
Double.Parse(txtBoxResult.Text)).ToString();
cmd.Parameters.AddWithValue("@results", txtBoxResult.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Data Saved Successfully", "info",
MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case "*":
txtBoxResult.Text = (resultValue *
Double.Parse(txtBoxResult.Text)).ToString();
cmd.Parameters.AddWithValue("@results", txtBoxResult.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Data Saved Successfully", "info",
MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
default:
break;
}
}

private void btnHistory_Click(object sender, EventArgs e)


{
Form2 History = new Form2();
History.Show();
}
}
}

You might also like