C# CheckListBox Kullanımı Örneği - Bilişim Konuları

C# CheckListBox Kullanımı Örneği

Bu yazımızda Visual C# Form ortamında Form nesnelerinden olan CheckListBox nesnesinin kullanımına ilişkin bir örnek program yapacağız. CheckListBox nesnesi bir liste içerisinde birden fazla onay düğmesini barındıran bir c# nesnesidir. Aşağıdaki örneğimizde bir CheckListBox nesnesi ile ilgili bazı işlemler yapılmaktadır. Bu işlemlerden bazıları şunlardır.

Tümünü seçme: Butona tıkladığımızda CheckListBox üzerinde bulunan tüm seçenekler seçili hale gelmektedir.

Seçileni kaldır: Seçileni kaldır butonuna tıklandığında daha önceden CheckListBox üzerinde bulunan ve seçili olan onay kutuların onayları kaldırılmaktadır.

Eleman Ekle: Eleman ekle butonuna tıklandığında CheckListBox nesnesine yeni bir seçenek eklenmektedir.

ListBox’a aktar: Bu butona tıkladığımızda CheckListBox nesnesinde bulunan ve seçili olan seçenekler yan tarafta bulunan liste kutusuna gönderilmektedir.

Ayrıca aşağıdaki etiketler aracılığı ile CheckListBox içerisinde bulunan toplama seçenek sayısını, CheckListBox üzerinde seçili olarak işaretlenmiş seçenek sayısını ve aranan seçeneğin CheckListBox nesnesi içerisinde bulunup bulunmadığını göstermektedir.

C# kodları:

//www.bilisimkonulari.com
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 CheckedListBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //CheckListBox'taki toplam eleman sayısı
        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkedListBox1.Items.Count; i++) 
            {
                checkedListBox1.SetItemChecked(i, true);
            }
        }

        //CheckListBox'taki seçili olan elemanların işaretini kaldırır
        private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                checkedListBox1.SetItemChecked(i, false);
            }
        }

        //CheckListBox'a eleman ekleme
        private void button3_Click(object sender, EventArgs e)
        {
            checkedListBox1.Items.Insert(3, "Hakkari");
        }

        //www.bilisimkonulari.com
        //CheckListBox'taki seçili elemanları ListBox'a aktarır.
        private void button4_Click(object sender, EventArgs e)
        {
            foreach(string item in checkedListBox1.CheckedItems)
            {
                listBox1.Items.Add(item);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //CheckListBox'ta bulunan eleman sayısını bulur.
            label1.Text = "Toplam Eleman Sayısı: "+checkedListBox1.Items.Count.ToString();

            //CheckListBox'ta aranan eleman olup olmadığını kontrol eder.
            bool aranilan = checkedListBox1.Items.Contains("İzmir");
            if (aranilan==true)
            {
                label3.Text = "Aranılan eleman bulundu";
            }
            else
            {
                label3.Text = "Aranılan eleman bulunamadı";
            }
        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //CheckListBox'ta seçili olan eleman sayısını bulur.
            label2.Text = "";
            label2.Text = "Seçili eleman sayısı: " + checkedListBox1.CheckedItems.Count;
        }
    }
}

Ekran görüntüleri:

checklistbox1

checklistbox2

checklistbox3

Bu Yazıya Tepkin Nedir?
+1
1
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Yorum Yap