具有搜索功能的 CheckedListBox 未正确检查

发布于 2024-12-10 01:32:43 字数 1329 浏览 0 评论 0原文

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

我想要的是能够搜索我的检查列表,同时保留那些具有之前已检查过。如果搜索结果中包含选中的项目,我希望对其进行选中。

该代码几乎可以运行!但由于某种原因,随机检查了一些框,并且它似乎通过调试工作,但当屏幕返回到控件时,它就不起作用了。

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

我的逻辑是:

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

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

非常感谢。

 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;
        }

I'm having strange issues with the check box control in C# .Net
My code below shows all logic that is required - _itemsChecked is a private dictionary containing all of the _fixtures and whether they are true or false (checked or un checked)

What I want is to be able to search my check list whilst retaining those which have been checked previously. If a checked item is included in the search results I want it to be checked.

The code nearly works! But for some reason boxes are randomly checked here and there, and it appears to work through debug but when the screen returns to the control it then hasn't worked.

Sure I'm missing something very simple.

My logic is:

DataSource includes those which match the typed search query,
Iterate through this list and check if the Guid is true in the dictionary.
If it is true then we set it as checked.

Hope I have provided adequate information.

Many thanks in advance.

 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;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吝吻 2024-12-17 01:32:43

所以我把我找到的几个例子放在一起。大部分工作来自如何我要让列表框刷新其项目文本吗?

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);
         }
      }
   }
}

So I put this together from a few examples I found. The majority of the work came from 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);
         }
      }
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文