C# Form kullanarak basit hesap makinesi uygulaması için tüm kodları ve adım adım yapılış aşamasını paylaştım. Ayrıca uygulamanın yapılışı ile ilgili video için aşağıdaki video bağlantısı ile basit hesap makinesi yapılışını inceleyebilirsiniz. Uygulama yapımında button sender ile ortak event kullanarak yazılacak kodun uzunluğunu azalttım. Hesap makinesini yaparken ondalık kısım ve temizleme ve hafızaya alma kısımları da yapıldığını belirmek isterim.
Uygulamanın çalışan hali aşağıdaki gibi olacaktır.
C# Basit Hesap Makinesi Yapımı
Adım 1: İlk olarak aşağıdaki gibi formu tasarlıyoruz. Label içindeki veriyi temizleyip, textbox içindeki değeri 0 (sıfır). Butonların isimleri önemli değil ancak label için lbSonuc, textbox için txtSonuc isimlerini kullandım.
Form üzerindeki kullandığım nesnelerin isimleri şu şekildedir.
- TextBox->txtSonuc
- Label ->lbSonuc
- Buttonlar (içindeki değerlere benzer şekilde)
- b0, b1, b2, b3, b4, b5, b6, b7, b8, b9
- bEkle, bCikar, bCarp, bBol
- bNotkta
- bEsit
- bC ve bCE
Adım 2: Uygulamdaki genel değişkenleri Form1.cs içine aşağıdaki gibi ekledim.
1 2 3 4 5 6 7 8 9 10 11 12 13 | namespace HesapMak { public partial class Form1 : Form { bool optDurum = false; //genel değişken double sonuc = 0; //genel değişken string opt = ""; //genel değişken ................ .............. ............ |
Adım 3: Rakamları seçtikten sonra olay sekmesine tıklayıp click olayına aşağıdaki gibi yazıp enter tuşuna basın
1 2 3 4 5 6 7 8 | if (txtSonuc.Text == "0" || optDurum) txtSonuc.Clear(); optDurum = false; Button btn = (Button)sender; txtSonuc.Text += btn.Text; |
Adım 4: İşlemleri butonlarını seçip, click olayına aşıdaki gibi kodları yazın.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | optDurum = true; Button btn = (Button)sender; string yeniOpt = btn.Text; lbSonuc.Text = lbSonuc.Text + " " + txtSonuc.Text + " "+yeniOpt; switch(opt) { case "+":txtSonuc.Text = (sonuc+ Double.Parse(txtSonuc.Text)).ToString(); break; case "-": txtSonuc.Text = (sonuc - Double.Parse(txtSonuc.Text)).ToString(); break; case "*": txtSonuc.Text = (sonuc * Double.Parse(txtSonuc.Text)).ToString(); break; case "/": txtSonuc.Text = (sonuc / Double.Parse(txtSonuc.Text)).ToString(); break; } sonuc = Double.Parse(txtSonuc.Text); txtSonuc.Text = sonuc.ToString(); opt = yeniOpt; |
Adım 5: CE butonunun click olayına aşağıdaki kodu yazın. C butonunun görevi txt içindeki değeri temizlemektir.
1 2 3 | txtSonuc.Text = "0"; |
Adım 6: C butonuna aşağıdaki kodu yazın. C butonu hem hazıdaki işlemi hem de ekrandaki işlemleri temizliyor.
1 2 3 4 5 6 7 | txtSonuc.Text = "0"; lbSonuc.Text = ""; opt = ""; sonuc = 0; optDurum = false; |
Adım 7: Eşittir butonuna aşağıdaki kodu yazın
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | lbSonuc.Text = ""; optDurum = true; switch (opt) { case "+": txtSonuc.Text = (sonuc + Double.Parse(txtSonuc.Text)).ToString(); break; case "-": txtSonuc.Text = (sonuc - Double.Parse(txtSonuc.Text)).ToString(); break; case "*": txtSonuc.Text = (sonuc * Double.Parse(txtSonuc.Text)).ToString(); break; case "/": txtSonuc.Text = (sonuc / Double.Parse(txtSonuc.Text)).ToString(); break; } sonuc = Double.Parse(txtSonuc.Text); txtSonuc.Text = sonuc.ToString(); opt = ""; |
Adım 8: Ondalık işlemleri için aşağıdaki kodu nokta(.) butonuna yazıp, uygulamayı çalıştırıp test edin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | if(txtSonuc.Text=="0") { txtSonuc.Text = "0"; } else if(optDurum) { txtSonuc.Text = "0"; } if(!txtSonuc.Text.Contains(",")) { txtSonuc.Text += ","; } optDurum = false; |
Uygulamadaki tüm kodlar:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | 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 BasitHesapMakinesi { public partial class Form1 : Form { Double sonuc = 0; bool optDurum = false; string opt = ""; //eski operatör bilgisi public Form1() { InitializeComponent(); txtSonuc.Text = "0"; } private void raklamOlay(object sender, EventArgs e) { if (txtSonuc.Text == "0" || optDurum) txtSonuc.Clear(); optDurum = false; Button btn = (Button)sender; txtSonuc.Text += btn.Text; } private void islemOlay(object sender, EventArgs e) { Button btn = (Button)sender; string yeniOpt = btn.Text; lbSonuc.Text = lbSonuc.Text +" "+ txtSonuc.Text+ " " + yeniOpt; //önceki operatör bilgisine ihtiyaç var. switch(opt) { case "+": txtSonuc.Text = (sonuc + Double.Parse(txtSonuc.Text)).ToString(); break; case "-": txtSonuc.Text = (sonuc - Double.Parse(txtSonuc.Text)).ToString(); break; case "/": txtSonuc.Text = (sonuc / Double.Parse(txtSonuc.Text)).ToString(); break; case "*": txtSonuc.Text = (sonuc * Double.Parse(txtSonuc.Text)).ToString(); break; default:break; } sonuc = Double.Parse(txtSonuc.Text); optDurum = true; opt = yeniOpt; } private void bCE_Click(object sender, EventArgs e) { txtSonuc.Text = "0"; } private void bC_Click(object sender, EventArgs e) { txtSonuc.Text = "0"; sonuc = 0; lbSonuc.Text = ""; opt = ""; } private void bEsit_Click(object sender, EventArgs e) { lbSonuc.Text = ""; optDurum = true; switch (opt) { case "+": txtSonuc.Text = (sonuc + Double.Parse(txtSonuc.Text)).ToString(); break; case "-": txtSonuc.Text = (sonuc - Double.Parse(txtSonuc.Text)).ToString(); break; case "/": txtSonuc.Text = (sonuc / Double.Parse(txtSonuc.Text)).ToString(); break; case "*": txtSonuc.Text = (sonuc * Double.Parse(txtSonuc.Text)).ToString(); break; default: break; } sonuc = Double.Parse(txtSonuc.Text); txtSonuc.Text = sonuc.ToString(); sonuc = 0; //atama yaptıktan sonra belleği boşaltıyor. opt = ""; } private void bNotkta_Click(object sender, EventArgs e) { if(txtSonuc.Text== "0") { txtSonuc.Text = "0"; } else if(optDurum) { txtSonuc.Text = "0"; } if (!txtSonuc.Text.Contains(",")) { txtSonuc.Text += ","; } optDurum = false; } } } |
Bu kodlar yazılırken hangi program kullanıldı, ben visual studio 2019 kullanıyorum ama bu kodlar uyuşmuyor, hatalı oluyor ?
VS 2015 ile yazıldı. Sürüm ile ilgili bir bir sorun olduğunu düşünmüyorum. Yazdığın kodları kontrol edebilir misin.
bunlar c# winformda yazilmis kodlar
şey ben visual studio 2019 kulanıyorum ama sizdeki bir çok şey benim programımda yok ne yapabilirim