Örnekte Combobox kullanımını ve özelliklerini bir uygulama üzerinde gerçekleştirdik.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//combobox içerisine iller eklendi.
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("İstanbul");
comboBox1.Items.Add("Ankara");
comboBox1.Items.Add("İzmir");
comboBox1.Items.Add("Aydın");
comboBox1.Text = comboBox1.Items[0].ToString();
}
// Yaz butonuna basıldığında label1 içerisine seçtiğimiz combobox elamanı yazılır.
private void button1_Click(object sender, EventArgs e)
{
label1.Text = comboBox1.Text;
}
//Combobox elemanları silmek için Temizle butonu
private void button2_Click(object sender, EventArgs e)
{
comboBox1.Items.Clear();
}
//Combobox içerisindeki elemanları bulmak için Şehir Sayısı butonu
private void button3_Click(object sender, EventArgs e)
{
label1.Text = comboBox1.Items.Count.ToString();
}
//Combobox içerisindeki bir elamanı bulmak için Ara butonu
private void button4_Click(object sender, EventArgs e)
{
if (comboBox1.Items.IndexOf(textBox2.Text) != -1)
MessageBox.Show("Eklemek istediğiniz şehir zaten var.");
else
comboBox1.Items.Add(textBox1.Text);
}
//Comboboxtaki elemanı bulup silme için Bul ve Sil butonu
private void button5_Click(object sender, EventArgs e)
{
if (comboBox1.Items.Contains(textBox2.Text) == true)
MessageBox.Show("Aradığınız Şehir listede vardır.");
else
MessageBox.Show("Aradığınız Şehir listede yoktur.");
}
}
}