1

C# .Net のチェック ボックス コントロールに奇妙な問題があり
ます。以下のコードは、必要なすべてのロジックを示しています。

私が欲しいのは、以前にチェックされたものを保持しながら、チェックリストを検索できるようにすることです。チェックした項目が検索結果に含まれていたらチェックしてもらいたい。

コードはほぼ機能します。しかし、何らかの理由でボックスがあちこちでランダムにチェックされ、デバッグでは機能しているように見えますが、画面がコントロールに戻ると機能しません。

確かに、非常に単純なものが欠けています。

私の論理は次のとおりです。

DataSource には、型指定された検索クエリに一致するものが含まれます。このリストを繰り返し処理し、辞書で Guid が true かどうかを確認します。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;
        }
4

1 に答える 1

0

そこで、見つけたいくつかの例からこれをまとめました。作業の大部分は、ListBox の項目テキストを更新するにはどうすればよいですか?

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);
         }
      }
   }
}
于 2011-10-14T16:03:27.930 に答える