![]() |
C# Switch Case Kullanımı Merhaba arkadaşlar bu yazımızda sizlere C# Switch Case yapısını kullanarak Gültepe Turizm adında bir uygulama yapmaya çalışacağız. Switch Case Kullanımı Switch Case deyimi de If else deyimleri ile aynı mantıkta işlemektedir. Switch Case deyimi genellikle karmaşık if else bloklarının yerine, daha okunabilir oldukları için tercih edilmektedir. Switch Case ile yapabileceğimiz her şeyi if else ile de yapabiliriz. Fakat if else ile yapılan her şeyi switch case ile yapamayız. Şimdi basit bir örnekle kullanımı açıklayalım daha sonra örneğimize geçelim. 1 2 3 4 5 6 7 8 9 10 11 12 13 int durum = 1; switch (durum) { case 1: MessageBox.Show("Durum 1");//durum değeri 1 ise bu bloğa girer ve bir alt satırdaki break komutuyla yapıdan çıkar break; case 2: MessageBox.Show("Durum 2");//durum değeri 2 ise bu bloğa girer ve bir alt satırdaki break komutuyla yapıdan çıkar break; default: MessageBox.Show("Yukarıdaki koşulların hiç biri desteklenmiyorsa");//durum 1 veya 2 değilse bu bloğa girer ve bir alt satırdaki break komutuyla yapıdan çıkar break; } Kodları vermeden önce uygulamayı biraz açıklayalım. Öncelikle Tur Seçinizden hangi tur seçilirse bir alt satırda olan gidilecek yerler ona göre değişmektedir. Daha sonra sırasıyla Ulaşım Tipini, Otel Tipini ve Kişi Sayılarını belirtip Hesapla butonuna tıklatıldığında bize tatilin maliyeti hesaplanmaktadır. Aynı işlemler yapılıp Rezervasyon Yap butonuna tıklatıldığında ise yapılmış olunan tercihler bir liste şeklinde sunulmuştur. [Üye Olmadan Linkleri Göremezsiniz. Üye Olmak için TIKLAYIN...] 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 135 136 137 138 139 140 141 142 143 144 public partial class Form1 : Form { public Form1() { InitializeComponent(); } int toplam = 0; int sec = 0; private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox1.SelectedIndex == 1) { listBox1.Items.Add(comboBox1.SelectedItem); sec = comboBox1.SelectedIndex; checkBox1.Text = "Topkapı Sarayı"; checkBox2.Text = "Anadolu Hisarı"; checkBox3.Text = "Adalar"; } if (comboBox1.SelectedIndex == 2) { listBox1.Items.Add(comboBox1.SelectedItem); sec = comboBox1.SelectedIndex; checkBox1.Text = "Irgandi Koprusu"; checkBox2.Text = "Uludağ Milli PArk"; checkBox3.Text = "Ulu Cami"; } if (comboBox1.SelectedIndex == 3) { listBox1.Items.Add(comboBox1.SelectedItem); sec = comboBox1.SelectedIndex; checkBox1.Text = "Odun Pazarı"; checkBox2.Text = "Uçak Müzesi"; checkBox3.Text = "Gondol Turu"; } } private void button1_Click(object sender, EventArgs e) { toplam = 0; switch (sec) { case 1: { if (checkBox1.Checked == true) { toplam += 10; listBox1.Items.Add(checkBox1.Text); } if (checkBox2.Checked == true) { toplam += 15; listBox1.Items.Add(checkBox2.Text); } if (checkBox3.Checked == true) { toplam += 20; listBox1.Items.Add(checkBox2.Text); } if (radioButton1.Checked == true) { toplam += 20; listBox1.Items.Add(radioButton1.Text); } else { toplam += 50; listBox1.Items.Add(radioButton2.Text); } if (radioButton3.Checked == true) { toplam += 10; listBox1.Items.Add(radioButton3.Text); } else if (radioButton4.Checked == true) { toplam += 25; listBox1.Items.Add(radioButton4.Text); } else { toplam += 45; listBox1.Items.Add(radioButton5.Text); } toplam = toplam * (Convert.ToInt16(textBox1.Text)); listBox1.Items.Add(textBox1.Text + " kişilik rezervasyom yapılmiştır"); break; } case 2: { if (checkBox1.Checked == true) toplam += 10; if (checkBox2.Checked == true) toplam += 15; if (checkBox3.Checked == true) toplam += 25; if (radioButton1.Checked == true) toplam += 25; else toplam += 45; if (radioButton3.Checked == true) toplam += 10; else if (radioButton4.Checked == true) toplam += 20; else toplam += 35; toplam = toplam * (Convert.ToInt16(textBox1.Text)); listBox1.Items.Add(textBox1.Text + " kişilik rezervasyon yapılmiştır"); break; } case 3: { if (checkBox1.Checked == true) toplam += 20; if (checkBox2.Checked == true) toplam += 30; if (checkBox3.Checked == true) toplam += 45; if (radioButton1.Checked == true) toplam += 45; else toplam += 65; if (radioButton3.Checked == true) toplam += 10; else if (radioButton4.Checked == true) toplam += 20; else toplam += 30; toplam = toplam * (Convert.ToInt16(textBox1.Text)); break; } } label8.Text = toplam.ToString(); } private void button2_Click(object sender, EventArgs e)//Rezervasyon Yap { listBox1.Visible = true; } } |
Tüm Zamanlar GMT +3 Olarak Ayarlanmış. Şuanki Zaman: 19:25. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO
Copyright ©2004 - 2025 IRCForumlari.Net Sparhawk