Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How do you bind data to a CheckedListBox?

How do you bind data to a CheckedListBox?

Scheduled Pinned Locked Moved C#
tutorialcsharpquestiondiscussion
2 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    inyoursadachine
    wrote on last edited by
    #1

    Hi, Can anyone tell me (via example is preferred) how to bind to a CheckedListBox in a Windows Form? All the docs I've seen allow you to bind to a control property (i.e. TextBox.Text) to an instance variable property. How do you bind to both the 'ItemText' and 'Checked' values for each Item in a CheckedListBox? TIA, Matt p.s. Also, I'm new here and I did look for a way to search all threads on the C# Discussion board but did not see how. Please advise.

    H 1 Reply Last reply
    0
    • I inyoursadachine

      Hi, Can anyone tell me (via example is preferred) how to bind to a CheckedListBox in a Windows Form? All the docs I've seen allow you to bind to a control property (i.e. TextBox.Text) to an instance variable property. How do you bind to both the 'ItemText' and 'Checked' values for each Item in a CheckedListBox? TIA, Matt p.s. Also, I'm new here and I did look for a way to search all threads on the C# Discussion board but did not see how. Please advise.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      The CheckedListBox does not support data-binding like that. First, you can add whatever objects you want to it. If the objects you're adding have ItemText and Checked properties, then you merely need to coordinate the event handlers for the CheckedListBox and set the Checked property of your object so that you can keep track of what's checked and what's not.

      public class MyItem
      {
      private string itemText;
      private bool checked;
      public MyItem() : this(null, false)
      {
      }
      public MyItem(string itemText, bool checked)
      {
      this.itemText = itemText;
      this.checked = checked;
      }
      public string ItemText
      {
      get { return this.itemText; }
      set { this.itemText = value; }
      }
      public bool Checked
      {
      get { return this.checked; }
      set { this.checked = value; }
      }
      }
      // ...
      public void AddItem(MyItem item)
      {
      int index = checkedListBox1.Items.Add(item);
      checkedListBox1.SetItemCheckState(index,
      item.Checked ? CheckState.Checked : CheckState.Unchecked);
      }
      // handler for CheckedListBox.ItemCheck
      private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
      {
      MyItem item = checkedListBox1.Items[e.Index];
      item.Checked = e.NewValue == CheckState.Checked;
      }

      You can always get the items out of CheckedListBox.Items whenever you want.

      Microsoft MVP, Visual C# My Articles

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups