Kalkulator Sederhana
Kalkulator Sederhana
Nama : Fadhil Dimas Sucahyo
NRP : 05111940000212
Untuk Pertemuan ini, saya telah membuat aplikasi kalkulator sederhana menggunakan .NET Framework.
Langkah pertama kita membuka project baru dalam aplikasi Visual Studio dan pilih Windows Forms App (.NET). Lalu kita dapat membuat beberapa pengaturan terhadap tampilan aplikasi, seperti menambahkan button, textbox, font, allignment, warna, dan lain-lainnya. Jangan lupa untuk merename button dan text yang dibuat menyesuaikan source code yang akan dibuat.
Berikut tampilan aplikasi yang telah dibuat.
- namespace SoapCalculator
- {
- public partial class Form1 : Form
- {
- Double resultValue = 0;
- String operationPerformed = "";
- bool isOperationPerformed = false;
- public Form1()
- {
- InitializeComponent();
- }
- private void button_Click(object sender, EventArgs e)
- {
- if ((textBox_Result.Text == "0") || (isOperationPerformed))
- textBox_Result.Clear();
- isOperationPerformed = false;
- Button button = (Button)sender;
- if (button.Text == ".")
- {
- if(!textBox_Result.Text.Contains("."))
- textBox_Result.Text = textBox_Result.Text + button.Text;
- }else
- textBox_Result.Text = textBox_Result.Text + button.Text;
- }
- private void operator_Click(object sender, EventArgs e)
- {
- Button button = (Button)sender;
- if (resultValue != 0)
- {
- button11.PerformClick();
- operationPerformed = button.Text;
- labelCurrentOperation.Text = resultValue + " " + operationPerformed;
- isOperationPerformed = true;
- }
- else
- {
- operationPerformed = button.Text;
- resultValue = Double.Parse(textBox_Result.Text);
- labelCurrentOperation.Text = resultValue + " " + operationPerformed;
- isOperationPerformed = true;
- }
- }
- private void button5_Click(object sender, EventArgs e)
- {
- textBox_Result.Text = "0";
- }
- private void button6_Click(object sender, EventArgs e)
- {
- textBox_Result.Text = "0";
- resultValue = 0;
- }
- private void button11_Click(object sender, EventArgs e)
- {
- switch(operationPerformed)
- {
- case "+":
- textBox_Result.Text = (resultValue + Double.Parse(textBox_Result.Text)).ToString();
- break;
- case "-":
- textBox_Result.Text = (resultValue - Double.Parse(textBox_Result.Text)).ToString();
- break;
- case "*":
- textBox_Result.Text = (resultValue * Double.Parse(textBox_Result.Text)).ToString();
- break;
- case "/":
- textBox_Result.Text = (resultValue / Double.Parse(textBox_Result.Text)).ToString();
- break;
- default:
- break;
- }
- resultValue = Double.Parse(textBox_Result.Text);
- labelCurrentOperation.Text = "";
- }
- }
- }
Contoh Hasil
Comments
Post a Comment