Yaz  Font K   lt Yaz  Font B y lt

ComboBox a Farklı Bir Metodla Items Eklemek

 

Merhaba arkadaşlar bu makalemizde comboBox nesnesine class metodunu kullanarak kişi adı itemsleri ekleyeceğiz. Formumuza 1 adet comboBox ve Button ekleyelim. comboBox ın DisplayMember özelliğini class kısmında tanımladığımız name i seçelim.

 

 

Resim1

Şekil 1

 

Form1.cs

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace add_item_to_combobox

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        class person

        {

            public string name { getset; }

            public string department { getset; }

        }

        private void btnAdd_Click(object sender, EventArgs e)

        {

            if (comboBox1.Items.Count > 0)

            comboBox1.Items.Clear();

            var personList = new List<person>();

            personList.Add(new person { name = "Bahadir", department = "IT" });

            personList.Add(new person { name = "Magda", department = "IT" });

            personList.Add(new person { name = "Mike", department = "Sales" });

            personList.Add(new person { name = "Teresa", department = "Sales" });

            personList.Add(new person { name = "Tom", department = "IT" });

            comboBox1.DataSource = personList;

            comboBox1.DisplayMember = "name";

        }

    }

}

      

Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN