Wednesday, 26 August 2015

Membuat Kalkulator Sederhana pada C#

Hasil Aplikasi :



Code program :





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Kalkulator
{
    public partial class Form1 : Form
    {
        string operation = "";
        Double value = 0;
        bool operation_pressed = false;

        public Form1()
        {
            InitializeComponent();

            this.Closing += new System.ComponentModel.CancelEventHandler(this.formCancel_Closing);
        }

        private void formCancel_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            string pesan = "Apakah Anda yakin ingin keluar?";
            string auth = "Konfirmasi";
            DialogResult konfirm = MessageBox.Show(pesan, auth, MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (konfirm == DialogResult.Yes)
            {
                Application.Exit();
            }
            else
            { e.Cancel = true; }
        }

        private void aboutMeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string pesan = "Nama : Iyunal Iqbal Kahfi XI-TKJ A / 13";
            string auth = "Author";
            MessageBox.Show(pesan, auth);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string pesan = "Apakah Anda yakin ingin keluar?";
            string auth = "Konfirmasi";
            DialogResult konfirm = MessageBox.Show(pesan, auth, MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (konfirm == DialogResult.Yes)
            {
                Application.Exit();
            }
            else
            { };
        }

        //function ON
        public void form_on()
        {
            MessageBox.Show("Nyalakan Terlebih Dahulu", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }
        private void On_Click(object sender, EventArgs e)
        {
            on.Text = "ON";
            textBox1.Text = "0";
            label99.Text = "0";
        }
        //END

        //function OFF
        private void button20_Click(object sender, EventArgs e)
        {
            on.Text = "OFF";
            textBox1.Clear();
            value = 0;
            textBox1.Text = "";
            label99.Text = "";
        }
        //END

        private void Clear_click(object sender, EventArgs e)
        {
            if (on.Text == "ON")
            {
                textBox1.Clear();
                value = 0;
                operator_label.Text = ".";
                textBox1.Text = "0";
                label99.Text = "0";
            }
            else
            {
                form_on();
            }
        }

        private void Button_Click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            if (on.Text == "ON")
            {
                if ((textBox1.Text == "0") || (operation_pressed))
                    textBox1.Clear();
                operation_pressed = false;
                textBox1.Text = textBox1.Text + b.Text;
            }
            else
            {
                form_on();
            }
        }

        private void Operator_Click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            if (on.Text == "ON")
            {
                if (value != 0)
                {
                    button14.PerformClick();
                    value = Double.Parse(textBox1.Text);
                    operation_pressed = true;
                    operation = b.Text;
                    label99.Text = value + "";
                    operator_label.Text = operation;
                }
                else
                {
                    value = Double.Parse(textBox1.Text);
                    operation_pressed = true;
                    operation = b.Text;
                    label99.Text = value + "";
                    operator_label.Text = operation;
                }
            }
            else
            {
                form_on();
            }
        }

        //function
        public double jumlah(double a, double b)
        {
            double hasil = a + b;
            return hasil;
        }
        public double kurang(double a, double b)
        {
            double hasil = a - b;
            return hasil;
        }
        public double kali(double a, double b)
        {
            double hasil = a * b;
            return hasil;
        }
        public double bagi(double a, double b)
        {
            double hasil = a / b;
            return hasil;
        }
        //end function

        private void button14_Click(object sender, EventArgs e)
        {
            //textBox1.Text = hasil(value, Double.Parse(textBox1.Text));
            if (on.Text == "ON")
            {

                double c, d;
                c = value;
                d = Double.Parse(textBox1.Text);

                switch (operation)
                {
                    case "+":
                        textBox1.Text = jumlah(c, d).ToString();
                        break;
                    case "-":
                        textBox1.Text = kurang(c, d).ToString();
                        break;
                    case "*":
                        textBox1.Text = kali(c, d).ToString();
                        break;
                    case "/":
                        textBox1.Text = bagi(c, d).ToString();
                        break;
                }
                //value = Int32.Parse(textBox1.Text);
                operation_pressed = false;
                operation = "";
            }
            else
            {
                form_on();
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (on.Text == "ON")
            {
                if (textBox1.Text.Length > 0)
                {
                    int panjang = textBox1.Text.Length;
                    textBox1.Text = textBox1.Text.Remove(panjang - 1, 1);
                    if (textBox1.Text == "")
                    {
                        textBox1.Text = "0";
                    }
                }
            }
            else
            {
                form_on();
            }
        }
    }
}

Artikel Terkait

Membuat Kalkulator Sederhana pada C#
4/ 5
Oleh

Berlangganan

Suka dengan artikel di atas? Silakan berlangganan gratis via email

Coretan Disqus

close
close