Buradasınız

C# Hesap Makinesi Yapma

windows form uygulamaları

C#'ta basit bir hesap makinesi yapalım.

Bir Windows forms Application (windows form uygulaması) oluşturularak aşağıdaki gibi bir tasarım oluşturalım.

c#'ta hesap makinesi yapma

Forumda bir tane TextBox ve 17 tane Button nesnesi bulunmaktadır. Formu oluşturduktan sonra Form1.cs dosyasına aşağıdaki kodlara arasına 

public partial class Form1 : Form
{

}

aşağıdaki kodları ekleyelim ve ilgili olaylara bağlayalım.

public double s1 = 0;
public string islem = null;
private void ekle(object sender, EventArgs e) //buttonların ( 0-9 ve , ) Click olayına bağlandı
{


    Button b = sender as Button;
    textBox1.Text += b.Text;

}

private void islemler(object sender, EventArgs e) //işlem buttonlarının Click olayına bağlandı
{
    Button b = sender as Button;
    s1 = double.Parse(textBox1.Text);
    if (b.Text == "+") islem = "+";
    else if (b.Text == "-") islem = "-";
    else if (b.Text == "*") islem = "*";
    else if (b.Text == "/") islem = "/";
    textBox1.Text = null;

}

private void button17_Click(object sender, EventArgs e) //Hesaplama buttonu ( = )
{
    double s2 = 0;
    s2 = double.Parse(textBox1.Text);

    if (islem=="+")
    {
        textBox1.Text = (s1 + s2).ToString();
    }
    else if (islem=="-")
    {
        textBox1.Text = (s1 - s2).ToString();
    }
    else if (islem == "/")
    {
        textBox1.Text = (s1 / s2).ToString();
    }
    else if (islem == "*")
    {
        textBox1.Text = (s1 * s2).ToString();
    }
}

private void button12_Click(object sender, EventArgs e) //Temizleme  button'u ( C ) 
{
    textBox1.Text = null;
    s1 = 0;
}

 

Yorum ekle

Konuyla İlgili Yazılar

GİU 2.Dönem Konu ve Uygulamaları

Gelişimiş internet uygulamaları dersi sınav konuları ve yapılan uygulamalar devamı...