YT.LI.GTH.IN.

Create Basic Calculator Using Windows Forms And C#

Proje Hakkında:

Bu kısımda toplama ,çıkarma,çarpma,bölme,karekök,üs alma,logaritma ve son olarak trigonomtrik ifadelerin değerlerini bulabilen bir kod dizisi ve aşamaları yer almaktadır.

				
					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 hessap
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
//Değer atamaları

        double sayi1, sayi2, sonuc;
        int secim = 0;

// Ondalıklı sayıları için virgül butonunu aktif etme fonksiyonu.

        private void button11_Click(object sender, EventArgs e)
        {
            if(textBox1.Text.IndexOf(",")<)
            {
                textBox1.Text = textBox1.Text + ",";
            }
        }

        
//Bütün rakamların basma fonksiyonu

        private void secili(object sender, EventArgs e)
        {
            if(textBox1.Text== "0")
            {
                textBox1.Text = "";
            }
            textBox1.Text = textBox1.Text + ((Button)sender).Text;
        }

//Bütün textboxu temizleme yani C butonu aktif etme fonksiyonu

        private void button16_Click(object sender, EventArgs e)
        {
            textBox1.Text = "0";
        }

//Del butonunu aktif etme fonksiyonu

        private void button17_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length-1);
            if(textBox1.Text=="")
            {
                textBox1.Text = "0";
            }
        }

       
//eşittir butonu aktif etme fonksiyonu 

        private void button18_Click(object sender, EventArgs e)
        {
            sayi2 = double.Parse(textBox1.Text);
            if (secim==1)
            {
                sonuc = sayi1 + sayi2;
                textBox1.Text = sonuc.ToString();  
            }
            if (secim == 2)
            {
                sonuc = sayi1 - sayi2;
                textBox1.Text = sonuc.ToString();
            }
            if (secim == 3)
            {
                sonuc = sayi1 / sayi2;
                textBox1.Text = sonuc.ToString("0.000");
            }
            if (secim == 4)
            {
                sonuc = sayi1 * sayi2;
                textBox1.Text = sonuc.ToString();
            }
            if (secim == 5)
            {
                sonuc = Math.Sqrt(sayi1);
                textBox1.Text = sonuc.ToString();
            }
            if (secim == 6)
            {
                sonuc = Math.Tan((Math.PI *sayi1) / 180);
                textBox1.Text = sonuc.ToString();
            }
            if (secim == 7)
            {
                sonuc = Math.Cos((Math.PI * sayi1) / 180);
                textBox1.Text = sonuc.ToString();
            }
            if (secim == 8)
            {
                sonuc = Math.Sin((Math.PI * sayi1) / 180);
                textBox1.Text = sonuc.ToString();
            }
            if (secim == 9)
            {
                sonuc = Math.Pow(sayi1 , sayi2);
                textBox1.Text = sonuc.ToString();
            }

            if (secim == 10)
            {
                sonuc = Math.Log(sayi1);
                textBox1.Text = sonuc.ToString();
            }


            if (secim == 11)
            {
                sonuc = (1 / Math.Tan((Math.PI * sayi1) / 180));
                textBox1.Text = sonuc.ToString();
            }


        }

//Keys kütüphanesi ile numPad ve klavye üstü sayıları aktif ederek klavye kullanılarak  //işlem yapmaya yaran kod bloğu.

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.NumPad1 || e.KeyCode==Keys.D1)
            {
                button1.PerformClick();
            }
            if (e.KeyCode == Keys.NumPad2 || e.KeyCode == Keys.D2)
            {
                button2.PerformClick();
            }
            if (e.KeyCode == Keys.NumPad3 || e.KeyCode == Keys.D3)
            {
                button3.PerformClick();
            }
            if (e.KeyCode == Keys.NumPad4 || e.KeyCode == Keys.D4)
            {
                button4.PerformClick();
            }
            if (e.KeyCode == Keys.NumPad5 || e.KeyCode == Keys.D5)
            {
                button5.PerformClick();
            }
            if (e.KeyCode == Keys.NumPad6 || e.KeyCode == Keys.D6)
            {
                button6.PerformClick();
            }
            if (e.KeyCode == Keys.NumPad7 || e.KeyCode == Keys.D7)
            {
                button7.PerformClick();
            }
            if (e.KeyCode == Keys.NumPad8 || e.KeyCode == Keys.D8)
            {
                button8.PerformClick();
            }
            if (e.KeyCode == Keys.NumPad9 || e.KeyCode == Keys.D9)
            {
                button9.PerformClick();
            }
            if (e.KeyCode == Keys.NumPad0 || e.KeyCode == Keys.D0)
            {
                button10.PerformClick();
            }
            if (e.KeyCode == Keys.Back)
            {
                button17.PerformClick();
            }
            if (e.KeyCode == Keys.Add)
            {
                button12.PerformClick();
            }
            if (e.KeyCode == Keys.OemMinus)
            {
                button13.PerformClick();
            }
        }

//toplama çıkarmaa ve diğer işlemlerin butonlarını aktif etme fonksiyonları

                private void button12_Click(object sender, EventArgs e)
        {
            sayi1 = double.Parse(textBox1.Text);
            textBox1.Text = "0";
            secim = 1;
        }
         private void button13_Click(object sender, EventArgs e)
        {
            sayi1 = double.Parse(textBox1.Text);
            textBox1.Text = "0";
            secim = 2;

        }

        private void button14_Click(object sender, EventArgs e)
        {
            sayi1 = double.Parse(textBox1.Text);
            textBox1.Text = "0";
            secim = 3;
        }

        private void button15_Click(object sender, EventArgs e)
        {
            sayi1 = double.Parse(textBox1.Text);
            textBox1.Text = "0";
            secim = 4;
        }
        private void button19_Click(object sender, EventArgs e)
        {
            sayi1 = double.Parse(textBox1.Text);
            textBox1.Text = "0";
            secim = 5;
        }

        private void button20_Click(object sender, EventArgs e)
        {
            sayi1 = double.Parse(textBox1.Text);
            textBox1.Text = "0";
            secim = 6;
        }

        private void button21_Click(object sender, EventArgs e)
        {
            sayi1 = double.Parse(textBox1.Text);
            textBox1.Text = "0";
            secim = 7;
        }

        private void button22_Click(object sender, EventArgs e)
        {
            sayi1 = double.Parse(textBox1.Text);
            textBox1.Text = "0";
            secim = 8;
        }

        private void button23_Click(object sender, EventArgs e)
        {
            sayi1 = double.Parse(textBox1.Text);
            textBox1.Text = "0";
            secim = 9;
        }

        private void button24_Click(object sender, EventArgs e)
        {
            sayi1 = double.Parse(textBox1.Text);
            textBox1.Text = "0";
            secim = 10;
        }

        private void button25_Click(object sender, EventArgs e)
        {
            sayi1 = double.Parse(textBox1.Text);
            textBox1.Text = "0";
            secim = 11;
        }


        
    }
}


				
			

Aşamalar:

Adım 1: Visual Studio’da Form oluşturmak
C# için en yaygın kullanılan program olan Visual Studio’yu hala bilgisayarınıza yüklemediyseniz buradan indirebilirsiniz. Az da olsa İngilizce biliyorsanız, dilini İngilizce kullanmanızı öneririz. Visual Studio’nun Türkçe versiyonu da bulunuyor. Visual Studio’nun Mac için uygun sürümü olsa da C#, Mac bilgisayarlarda çalışmıyor. Windows işletim sistemine ihtiyacınız var. Visual Studio’yu açtıktan sonra New Project’i seçerek Windows Forms Application oluşturun. Ekranda karşınıza boş bir Form1 kutucuğu çıkacak.

Adım 2: Ögeleri yerleştirmek
    Ekranın sol tarafında Toolbox (Araçlar), sağ tarafındaysa Properties (Özellikler) kısımlarını görüyorsunuz. Sağda Form1’in özelliklerinden formunuzun adını Hesap Makinesi olarak değiştirebilir, isterseniz bir ikon yerleştirebilirsiniz. Hesap makinesinin düzenlenmesi için özellikler bölümünde bazı değişiklikler yapabilirsiniz. Örneğin, StartPosition özelliğini CenterScreen olarak değiştirirseniz hesap makinenizi çalıştırdığınızda ekranın ortasında çıkacaktır. FormBorderStyle özelliğini FixedDialog olarak değiştirmek hesap makinenizin boyutunu sabitlemenize yarar. Ancak bu yazıda hesap makinesinin kodlarına ağırlık vereceğimiz için bu özellikleri şimdilik kısa kesiyoruz. Sol taraftaki Toolbox’tan sürükle – bırak yaparak bir TextBox, ve bir Button ekleyin ve butonun boyutunu, yazı tipini, yazı boyutunu istediğiniz şekilde düzenleyin. Daha sonra butonu kopyala – yapıştır yaparak aşağıdaki şekilde yerleştirin. Butonların üzerinde yazan rakam ve sembolleri Text özelliğini değiştirerek yazabilirsiniz. Sonunda bir hesap makinesi görüntüsü elde etmeniz gerekiyor.

Adım 3: Kodlama
    Şimdi sıra geldi kod yazma kısmına. C# bilgi ve becerilerinizi geliştirdikçe farklı hesap makinesi kodları keşfedebilir ya da deneyebilirsiniz. Biz bu yazıda, en basit C# hesap makinesi kodlarını sizinle paylaşacağız. Visual Studio’da kod yazmak için ögelere çift tıklamanız yeterlidir. Ögelere tıklandığında ne yapılmasını, bu ögede hangi işlemin gerçekleşmesini istiyorsanız bunun kodunu yazacağınız alan açılır. Aşağıda Button1, Button2 şeklinde devam eden kısım sizin butonları sıralamanıza göre değişiklik gösterebilir. Buton ile kodu doğru şekilde eşleştirdiğinizden emin olun. Kodlama hata yapmadan öğrenilmez! Hata yapmaktan korkmayın. C# öğrenmeye kararlıysanız Fahrettin Erdinç’in hazırladığı C# ile Nesne Tabanlı Programlama adlı kitap ile yolunuzda daha emin adımlarla ilerleyebilirsiniz. Eğitim videolarıyla desteklenen kitap programlamayı sıfırdan öğrenmek isteyenler için harika bir kaynak.

Leave a Comment