【问题标题】:Make CheckedListBox remember CheckedItems after Filter使 CheckedListBox 在过滤后记住 CheckedItems
【发布时间】:2015-11-03 16:49:39
【问题描述】:

我在尝试让 CheckedListBox 记住检查的项目时遇到问题。在这个程序中,我将 DataTable 中的项目加载到 CheckedListBox 它第一次工作,但在指定过滤器后,CheckedListBox 忘记了所有检查的项目。 那么问题来了,在get应用后如何让它记住勾选的项目呢?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AppNumber302
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        DataTable dt = new DataTable();

        private void Form1_Load(object sender, EventArgs e)
        {
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            dt.Columns.Add("checked");

            dt.Rows.Add(10, "azer1", true);
            dt.Rows.Add(10, "azer2", true);
            dt.Rows.Add(10, "azer3", false);
            dt.Rows.Add(10, "azer4", false);

            checkedListBox1.DataSource = dt;
            checkedListBox1.DisplayMember = "name";
            checkedListBox1.ValueMember = "id";

            PerformCheck();
        }

        private void PerformCheck()
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow drv = dt.Rows[i];
                bool stat = bool.Parse(drv[2].ToString());
                checkedListBox1.SetItemChecked(i, stat);
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataRow[] dr = dt.Select("name LIKE '%" + textBox1.Text + "%'");
                DataTable tmp = dr.CopyToDataTable();
                checkedListBox1.DataSource = tmp;
                checkedListBox1.ValueMember = "id";
                checkedListBox1.DisplayMember = "name";
            }
            catch
            {
                checkedListBox1.DataSource = new DataTable();
            }
        }
    }
}

【问题讨论】:

    标签: c# .net checkedlistbox listboxitems


    【解决方案1】:

    我一直在寻找类似问题的解决方案,最后我自己解决了,我知道这篇文章很旧,但我希望这可以帮助某人。

    您可以创建一个列表来保存已检查的项目,以便在重新绑定时再次检查它们。 我将向您展示我的代码中的一个示例

    List<object> checkedItems = new List<object>(); //List to save checked items
    object[] checkListBoxItems = new object[]       //CheckedListBox items
    {
    "item1","item2","item3","item4","item5"
    }
    //Method called when item is checked in checkedListBox
    private void CheckedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if(e.NewValue == CheckState.Checked)
            {
                //If item is checked and not already on list, we add it to de list
                //With this filter we prevent adding the same item multiple times on rebind
                if (!checkedItems.Contains(checkedListBox1.Items[e.Index]))
                {
                    checkedItems.Add(checkedListBox1.Items[e.Index]);
                }
            } 
            else
            {
                //If not checked, we remove it from list
                checkedItems.Remove(checkedListBox1.Items[e.Index]);
            }
        }
    
    private void TextBox1_TextChanged(object sender, EventArgs e)
        {
            //Start update of checkedlistbox and clear items to refill
            checkedListBox1.BeginUpdate();
            checkedListBox1.Items.Clear();
            //If there is any text on the filter field
            if (!string.IsNullOrEmpty(textBox1.Text))
            {   
                //We check every item on our original item list (object array)
                foreach(object item in checkListBoxItems)
                {
                    //Check if string in textfield matches item (case sensitive) 
                    //and adds it to checkedListBox. At this point we should see the item
                    if (item.ToString().Contains(textBox1.Text))
                    {
                        checkedListBox1.Items.Add(item);
                    }
                }
            }
            else
            {
                //If filter string is empty, we add the whole array of items to the list
                checkedListBox1.Items.AddRange(checkListBoxItems);
            }
            //Now checkedListBox is filled with the filtered(or not) items, its time
            //to check if they where checked previously and check them in the new list
            if(checkedClients.Count > 0)
            {
                foreach (object item in checkedListBoxItems)
                {
                    if (checkedItems.Contains(item))
                    {
                        //If item is shown in the list atm (otherwise it will crash)
                        if(checkedListBox1.Items.Contains(item)
                        {
                             checkedListBox1.SetItemChecked(checkedListBox1.Items.IndexOf(item), true);
                        }
    
                    }
                }
            }
    
            checkedListBox1.EndUpdate();
        }
    

    【讨论】:

      【解决方案2】:

      您正在重新绑定复选框列表(当您设置 DataSource 属性时)。因此,您之前在 PerformCheck 中所做的工作已不复存在。一个简单的选项是在 TextChanged 处理程序中再次调用该方法。

      【讨论】:

      • 即使再次调用它,在检查或取消检查之后,CheckedListBox 也会忘记检查/取消检查的项目。
      • 啊。让 PerformCheck 接受一个数据表作为参数,并在 Load 中传入 dt,在 TextChanged 中传入 tmp。一个很好的理由不在私有方法中使用类变量。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      • 2019-05-14
      • 1970-01-01
      • 2015-07-14
      相关资源
      最近更新 更多