【问题标题】:CheckedListBox with search function not checking correctly具有搜索功能的 CheckedListBox 未正确检查
【发布时间】:2011-12-07 18:45:18
【问题描述】:

我在使用 C# .Net 中的复选框控件时遇到了奇怪的问题
我下面的代码显示了所需的所有逻辑 - _itemsChecked 是一个私有字典,包含所有 _fixture 以及它们是真还是假(选中或未选中)

我想要的是能够搜索我的检查列表,同时保留之前检查过的那些。如果搜索结果中包含选中的项目,我希望它被选中。

代码几乎可以正常工作!但是由于某种原因,这里和那里随机检查框,它似乎可以通过调试工作,但是当屏幕返回到控件时,它就没有工作了。

当然,我错过了一些非常简单的东西。

我的逻辑是:

DataSource 包括与键入的搜索查询匹配的那些, 遍历此列表并检查字典中的 Guid 是否为真。 如果它是真的,那么我们将它设置为选中。

希望我提供了足够的信息。

非常感谢。

 private void searchTextBox_KeyUp(object sender, EventArgs e)
        {
            lst.DataSource = _fixtures
                .OrderBy(f => 
                    f.Description)
                .Where(f => 
                    f.Description.ToLower().Contains(searchFixturesTextBox.Text.ToLower()))
                .ToList();

            lst.DisplayMember = "Description";

            for (var i = 0; i < lst.Items.Count; i++)
                if(_itemsChecked.Contains(new KeyValuePair<Guid, bool>(((Fixture)lst.Items[i]).Guid, true)))
                    lst.SetItemChecked(i, true);
        }

        void lst_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            var selectedItem = ((ListBox) sender).SelectedItem as Fixture;

            if (selectedFixtureItem != null)
                _itemsChecked[selectedItem.Guid] = e.CurrentValue == CheckState.Unchecked;
        }

【问题讨论】:

  • _itemsChecked.Contains(new KeyValuePair&lt;Guid, bool&gt;(((Fixture)lst.Items[i]).Guid, true)) - 看起来效率低下,我认为 LINQ 可能会遍历整个集合。如果这是一个“普通”的 .net 字典,为什么不定义 bool isUnchecked; 然后使用:_itemsChecked.TryGetValue(((Fixture)lst.Items[i]).Guid, out isUnchecked) &amp;&amp; isUnchecked 代替?
  • 我已经解决了这个问题,但经验太少,无法完整发布答案。我会在星期一这样做。 selectedItem = ((ListBox) sender).SelectedItem as Fixture;改为var selectedFixtureItem = lstFixtures.Items[e.Index] as Fixture;

标签: c# .net dictionary checkedlistbox


【解决方案1】:

所以我从我找到的几个例子中把它放在一起。大部分作品来自How do I make a ListBox refresh its item text?

public class Employee
{
   public string Name { get; set; }
   public int Id { get; set; }
   public bool IsChecked { get; set; }

   public override string ToString()
   {
      return Name;
   }
}

public partial class Form1 : Form
{
   // Keep a bindable list of employees
   private BindingList<Employee> _employees;

   public Form1()
   {
      InitializeComponent();
      // Load some fake employees on load
      this.Load += new EventHandler(Form1_Load);
      // Click once to trigger checkbox changes
      checkedListBox1.CheckOnClick = true;
      // Look for item check change events (to update there check property)
      checkedListBox1.ItemCheck += 
         new ItemCheckEventHandler(CheckedListBox_ItemCheck);
   }

   // Load some fake data
   private void Form1_Load(object sender, EventArgs e)
   {
      _employees = new BindingList<Employee>();
      for (int i = 0; i < 10; i++)
      {
         _employees.Add(new Employee() 
            { Id = i, Name = "Employee " + i.ToString() });
      }

      // Display member doesnt seem to work, so using ToString override instead
      //checkedListBox1.DisplayMember = "Name";
      //checkedListBox1.ValueMember = "Name";
      checkedListBox1.DataSource = _employees;

      // Another example databind to show selection changes
      txtId.DataBindings.Add("Text", _employees, "Id");
      txtName.DataBindings.Add("Text", _employees, "Name");
   }

   // Item check changed, update the Employee IsChecked property
   private void CheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
   {
      CheckedListBox clb = sender as CheckedListBox;
      if (clb != null)
      {
         Employee checked_employee = clb.Items[e.Index] as Employee;
         if (checked_employee != null)
         {
            checked_employee.IsChecked = (e.NewValue == CheckState.Checked);
         }
      }
   }

   // Just a simple test that removes an item from the list, rebinds it
   // and updates the selected values
   private void btnChangeList_Click(object sender, EventArgs e)
   {
      _employees.RemoveAt(1);
      checkedListBox1.DataSource = _employees;

      for (var i = 0; i < checkedListBox1.Items.Count; i++)
      {
         Employee employee_to_check = checkedListBox1.Items[i] as Employee;
         if (employee_to_check != null)
         {
            checkedListBox1.SetItemChecked(i, employee_to_check.IsChecked);
         }
      }
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    相关资源
    最近更新 更多