c# yeni başlayanlar için
Merhaba arkadaşlar,
C#’a yeni başlayanlar için standart proje olan Hesap Makinesi projesini sizlere anlatacağım.
Bu yazının amacı c# a yeni başlayanlar c# ın çalışma mantığını ve bazı kodları tanıtmaktır.
Şimdi bir WindowsFormApplications oluşturalım isterseniz projenizin adını “hesapmakinesi” yapabilirsiniz. Ben projenin adını hesapmakinesi yapıyorum.
Proejemiz açıldığında karşımıza boş bir form gelicek.
Sıtandart hesap makinelerinin yeni öğrenenlerin görüşlerini kısıtladığına inandığım için biz daha gelişmiş bir hesap makinesi yapacağız.
ben aşağıdaki resimdeki gibi bir form hazırladım
Yukarıdaki form elemanlarının özelliklerini hemen açıklıyalım
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
|
//text'i 0 olan buton
name=sifir
//text'i 1 olan buton
name=bir
//text'i 2 olan buton
name=iki
//text'i 3 olan buton
name=uc
//text'i 4 olan buton
name=dort
//text'i 5 olan buton
name=bes
//text'i 6 olan buton
name=alti
//text'i 7 olan buton
name=yedi
//text'i 8 olan buton
name=sekiz
//text'i 9 olan buton
name=dokuz
//text'i = olan buton
name=esittir
//text'i x olan buton
name=carp
//text'i / olan buton
name=bol
//text'i + olan buton
name=topla
//text'i - olan buton
name=cikar
//textbox1
name=textBox1
Enabled=False
TextAligin=right
//textbox2
name=textBox2
Enabled=False
TextAligin=right
//text'i sonuc olan Label
name=Label1
//text'i 0 olan Label
name=sonuc
|
Evet arkadaşları bu ayarları form üzerindeki elemanlara tıkladıktan sonra sağ tarafta bulunan properties alanından bularak yapabilirsiniz.
Şimdi yavaş yavaş kodlamaya geçelim
Butonlara tıklandığında tıklanılan butonun üzerindeki yazının textBox1’e geçmesini sağlıyacağız.Bunun için form üzerindeki butonlara ikikez tıklayalım.
ben ilk 0 butonuna tıkladım. ve açılan sayfaya aşağıdaki kod eklendi
1
2
3
4
|
private void sifir_Click(object sender, EventArgs e)
{
}
|
Bu kodun anlamı şu. Name değeri sifir olan butonun üzerine tıklanıldığında(Yani click olayında) aşağıdaki parantez içindeki kodları çalıştır demektir.
Şimdi içine aşağıdaki kodları ekliyelim
1
|
textBox2.Text += "0";
|
Şimdi bu kodu açıklayalım. Bu kod textBox1’e içindeki önceden olan bilgiyideğiştirmeden sonuna 0 ekle demek.
Bunu aynı şekilde diğer butonlarada uygulayalım.
Evet Name değeri esittir olan buton haricinde bütün butonlara aynı işleme uyguladık ve şuan yaptığımız kodlama aşağıdaki gibi olucaktır.
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
|
private void sifir_Click(object sender, EventArgs e)
{
textBox2.Text += "0";
}
private void bir_Click(object sender, EventArgs e)
{
textBox2.Text += "1";
}
private void iki_Click(object sender, EventArgs e)
{
textBox2.Text += "2";
}
private void uc_Click(object sender, EventArgs e)
{
textBox2.Text += "3";
}
private void dort_Click(object sender, EventArgs e)
{
textBox2.Text += "4";
}
private void bes_Click(object sender, EventArgs e)
{
textBox2.Text += "5";
}
private void alti_Click(object sender, EventArgs e)
{
textBox2.Text += "6";
}
private void yedi_Click(object sender, EventArgs e)
{
textBox2.Text += "7";
}
private void sekiz_Click(object sender, EventArgs e)
{
textBox2.Text += "8";
}
private void dokuz_Click(object sender, EventArgs e)
{
textBox2.Text += "9";
}
|
evet şimdi ufak bir test yapalım
Şimdi sıra geldi esittir butonuna.
Evet şimdi burada yapmamız gereken işi planlıyalım. Burada textBox1 de bir değer varmı yokmu diye kontrol edicez sonrada bir işlem varmı diye kontrol edicez. sonrada işlemlere göre sonuç yazdırıcaz.
şimdi aşağıdaki kodu inceliyelim
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
private void sifir_Click(object sender, EventArgs e)
{
textBox2.Text += "0";
}
private void bir_Click(object sender, EventArgs e)
{
textBox2.Text += "1";
}
private void iki_Click(object sender, EventArgs e)
{
textBox2.Text += "2";
}
private void uc_Click(object sender, EventArgs e)
{
textBox2.Text += "3";
}
private void dort_Click(object sender, EventArgs e)
{
textBox2.Text += "4";
}
private void bes_Click(object sender, EventArgs e)
{
textBox2.Text += "5";
}
private void alti_Click(object sender, EventArgs e)
{
textBox2.Text += "6";
}
private void yedi_Click(object sender, EventArgs e)
{
textBox2.Text += "7";
}
private void sekiz_Click(object sender, EventArgs e)
{
textBox2.Text += "8";
}
private void dokuz_Click(object sender, EventArgs e)
{
textBox2.Text += "9";
}
private void esittir_Click(object sender, EventArgs e)
{
//farklı işlem
if (textBox1.Text.Length > 2)//textBox1 de iki sayı ve işlem
{
if (textBox1.Text.Contains("+") == false && textBox1.Text.Contains("-") == false && textBox1.Text.Contains("x") == false && textBox1.Text.Contains("/") == false)
{
textBox1.Text = textBox2.Text;
sonuc.Text = textBox2.Text;
}
else
{
}
}
}
private void carp_Click(object sender, EventArgs e)
{
if (textBox2.Text.Length == 0) { return; }
if (textBox1.Text.Length == 0)
{
textBox1.Text = textBox2.Text;
}
else
{
textBox1.Text += "x" + textBox2.Text;
}
int islem = Convert.ToInt16(sonuc.Text) * Convert.ToInt16(textBox2.Text);
sonuc.Text = Convert.ToString(islem);
textBox2.Text = "";
}
private void bol_Click(object sender, EventArgs e)
{
if (textBox2.Text.Length == 0) { return; }
if (textBox1.Text.Length == 0)
{
textBox1.Text = textBox2.Text;
}
else
{
textBox1.Text += "/" + textBox2.Text;
}
int islem = Convert.ToInt16(sonuc.Text) / Convert.ToInt16(textBox2.Text);
sonuc.Text = Convert.ToString(islem);
textBox2.Text = "";
}
private void topla_Click(object sender, EventArgs e)
{
if (textBox2.Text.Length == 0) { return; }
if (textBox1.Text.Length == 0)
{
textBox1.Text = textBox2.Text;
}
else
{
textBox1.Text += "+" + textBox2.Text;
}
int islem = Convert.ToInt16(sonuc.Text) + Convert.ToInt16(textBox2.Text);
sonuc.Text = Convert.ToString(islem);
textBox2.Text = "";
}
private void cikar_Click(object sender, EventArgs e)
{
if (textBox2.Text.Length == 0) { return; }
if (textBox1.Text.Length == 0)
{
textBox1.Text = textBox2.Text;
}
else
{
textBox1.Text += "-" + textBox2.Text;
}
int islem = Convert.ToInt16(sonuc.Text) - Convert.ToInt16(textBox2.Text);
sonuc.Text = Convert.ToString(islem);
textBox2.Text = "";
}
|
yıkarıdaki kodları genel olarak inceliyelim şimdi.
1
|
if (textBox2.Text.Length == 0) { return; }
|
bu kodun amacı, bir sayı eklendiğinde sayının textBox2’ye eklendiğinden bir kontrol amacı vardır. Bu kontrol işlemi şu şekildedir. textBox2 de herhangi bir değer varsa yani textBox2 nin içindeki metin uzunluğu eğer 0 değilse kodlara devam et.
1
2
3
4
5
6
7
8
|
if (textBox1.Text.Length == 0)
{
textBox1.Text = textBox2.Text;
}
else
{
textBox1.Text += "-" + textBox2.Text;
}
|
Bu kodun amacı ise işaretleri düzenlemektir. Eğer bu if şartı yerine sadece textBox1.Text += “-” + textBox2.Text; olsaydı textBox1’e yazılan veriler şu şekilde olurdu.
1
|
+1+10-2x3/3
|
ancak bu kod sayesinde textBox1’e yazılan veriler
1
|
1+10-2x3/3
|
oluyor. Yani en baştaki matematiği katleden işaret kalkıyor.
Ve son olarakta İşlemin yapıldığı ve sonucun yazıldığı kısıma geldik.
1
2
3
4
|
int islem = Convert.ToInt16(sonuc.Text) - Convert.ToInt16(textBox2.Text);
sonuc.Text = Convert.ToString(islem);
textBox2.Text = "";
|
Burada yaptığımız şey son yapılan işlemin sonucunu alıp yeni eklenen işleme dahil ederek sonucu güncellemek
yani basit bir şekilde anlatmak gerekirse 1+2+3 işlemi zaten yapılmış olsun sonuç şuan 6 bu işleme bir değer daha girip işlemi 1+2+3-4 yaparsak işlemi şu şekilde parantez içine aldığımızda (1+2+3)-4 = 6-4 = 2 den yeni sonuç 2 oluyor.
Projeyi indirme linki Projeyi İndir
Leave a reply