Merhaba arkadaşlar bu yazımda sizlerle birden fazla else if kullanımından bahsetmek istiyorum. Konu proje üzerinde çok daha rahat anlaşılıyor. O yüzden hemen kodlara geçelim.
Projemize hemen bir winform ekleyelim ve içerisine ad ve soyad diye 2 tane textbox ve bir tane de button ekleyelim
Şimdi kaydet butonuna çift tıklayarak kod bölümüne geçelim.
1 2 3 4 5 6 7 8 9 10 11 |
private void simpleButton1_Click(object sender, EventArgs e) { if (txtad.Text=="") { //Eğer txtad metin kutusu boş ise çalışacak kısım burası... } else { //Eğer txtad metin kutusu boş değil ise çalışacak kısım burası } } |
Yorumlarla açıklamaya çalıştım kodun çalışma prensibini…
1 2 3 4 5 6 7 8 9 10 |
if (txtad.Text == "") { //Eğer txtad metin kutusu boş ise çalışacak kısım burası... MessageBox.Show("Ad alanını boş bıraktınız", "UYARI", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); } else { //Eğer txtad metin kutusu boş değil ise çalışacak kısım burası MessageBox.Show("Alanı doldurdunuz", "UYARI", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); } |
Şimdi ise hem ad alanı hem de soyad alanı boş bırakılınca uyarı verdirelim.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
private void simpleButton1_Click(object sender, EventArgs e) { if (txtad.Text == "" || txtsoyad.Text=="") { //Eğer txtad metin kutusu ve txtsoyad metin kutusu boş ise çalışacak kısım burası... MessageBox.Show("Ad ve soyad alanını boş bıraktınız", "UYARI", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); } else { //Eğer txtad metin kutusu ve txtsoyad metin kutusu boş değil ise çalışacak kısım burası MessageBox.Show("Sorun yok", "UYARI", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); } } |
Burada ise ad alanı VEYA soyad alanı boş ise uyarı verdirelim.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
private void simpleButton1_Click(object sender, EventArgs e) { if (txtad.Text == "" && txtsoyad.Text == "") { //Eğer txtad veya txtsoyad metin kutusu boş ise çalışacak kısım burası... MessageBox.Show("Ad veya soyad alanını boş bıraktınız", "UYARI", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); } else { //Eğer txtad veya txtsoyad metin kutusu boş değil ise çalışacak kısım burası MessageBox.Show("Sorun yok", "UYARI", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); } } |
Umarın olayı anlatabilmişimdir.
İyi kodlamalar.