Yeni bir form açıyoruz ve formumuza CheckBox ile Button ekliyoruz.
CheckBox
Buttonun kod satırına
private void button1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
MessageBox.Show("Seçili");
}
else
{
MessageBox.Show("Seçili Değil");
}
}
//If ile checkBox'ın seçili olup olmadığını kontrol ediyoruz(Checked özelliği).Checked true ise seçili demektir.
Formumuza 2 Adet radiobutton, bir textbox ve button ekleyelim.Radiobuttonların name'leri rb_bay, rb_bayan ve textlerinde bay, bayan yazsın. textboxin name'i txt_isim. Button'un textinde ise göster yazalım.
RadioButton
Buttonun kod satırına
private void button2_Click(object sender, EventArgs e)
{
if (rb_bay.Checked == true)
{
MessageBox.Show(txt_isim.Text + " Bey");
}
else if (rb_bayan.Checked == true)
{
MessageBox.Show(txt_isim.Text + " Hanım");
}
else
{
MessageBox.Show("seçili değil");
}
}
//If ile rb_bay'ın checked(seçili) özelliğini kontrol ediyoruz. Eğer True(doğru) ise messagebox'da txt_isim'den gelen değer + bey yazıyoruz.
//Else if'de ise rb_bayan için checked'ı kontrol ediyoruz.
/Else'de iki durumda sağlanmaz ise(rb_bay veya rb_bayan seçilmez ise) seçili değil messajını gösteriyoruz.
Formumuza 2 adet checkbox ve bir button ekliyoruz. Checkboxların name'leri cb_ogrenci ve cb_programci, textleri ise Öğrence ve programcı olsun. Button'un text'inde Burs Kontrol yazalım.
Buttonun kod satırına
private void button3_Click(object sender, EventArgs e)
{
if (cb_ogrenci.Checked == true)
{
if (cb_programci.Checked == true)
{
MessageBox.Show("kazandınız");
}
else
{
MessageBox.Show("kazanamadınız");
}
//IF bloğunun içine ayrı bir if daha ekledik. Bu sayede cb_ogrenci checked durumunda cb_programcı'nın da chehcked olup olmadığını kontrol edebiliyoruz.
}
else if (cb_programci.Checked == true)
{
MessageBox.Show("kazanamadınız");
}
else
{
MessageBox.Show("seçili değil");
}
}
//If ile cb_ogrenci ve cb_programcı'nın checked özelliğini kontrol ediyoruz. Eğer ikiside tiklenmis ise kazandınız. Sadece biri tikli ise kazanamadınız uyarısı veriyor. Hiç biri tikli değil ise seçili değil uyarısı veriyor.
Yeni bir button ekliyoruz.
private void button4_Click(object sender, EventArgs e)
{
if (cb_ogrenci.Checked == true && cb_programci.Checked == true)
{
MessageBox.Show("kazandınız");
}
else if (cb_programci.Checked == true)
{
MessageBox.Show("kazanamadınız");
}
else if (cb_ogrenci.Checked == true)
{
MessageBox.Show("kazanamadınız");
}
else
{
MessageBox.Show("secili değil");
}
}
//Yukarda yaptığımız burs kazanma örneğnin farklı bir sekilde yapılışı. Burda IF içine yeni bir if yazmaktansa &&(ve operatörü) ile 2'li seçim durumunu kontrol ediyoruz.