title: c# How to set check state of items in a checked listbox if it matches an item in a listbox
body: <p><a href="https://i.stack.imgur.com/HEFuz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HEFuz.png" alt="enter image description here"></a></p>
  
  <p>Here's what I've been trying to do:</p>
  
  <p>If i add members to listbox1, a variable will store those items and turn them into a single string using <em>stringbuilder</em>, in which the string will appear as an item on listbox2. I got this working already.</p>
  
  <p>I want to display the selected item on listbox2 to listbox1 as separated string/item which I already got working, but I also want to check all the items on my checklistbox1 that matches the items on listbox1, in which I'm having trouble with since only 1 item is checked on checklistbox1 using for loop. </p>
  
  <p>Hope someone can help me on this one..</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  
  namespace ADDPAGE2
  {
      public partial class Form5 : Form
      {
          public Form5()
          {
              InitializeComponent();
          }
  
          public string s;
  
          private void button1_Click(object sender, EventArgs e)
          {
  
              listBox1.Items.Clear();
              //code for adding authors to the listbox
              for (int i = 0; i &lt; checkedListBox1.Items.Count; i++)
              {
                  if (checkedListBox1.GetItemChecked(i))
                  {
                      string str = (string)checkedListBox1.Items[i];
                      listBox1.Items.Add(str);
                  }
              }
  
  
              //for making a single string variable from all checked items in checkedlistbox
              StringBuilder sb = new StringBuilder();
              foreach (object o in listBox1.Items)
              {
                  sb.AppendLine(o.ToString() + ",");
              }
              s = sb.ToString();
              listBox2.Items.Add(s);
  
          }
  
          private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
          {
  
              listBox1.Items.Clear();
              s = listBox2.SelectedItem.ToString();
              string[] tokens = s.Split(',');
              for (int y = 0; y &lt; tokens.Count(); y++)
              {
                  listBox1.Items.Add(tokens[y]);
  
                  //loop code for checking matching items between listbox and checkedlistbox
                  for (int item = 0; item &lt; listBox1.Items.Count; item++)
                  {
                      for (int i = 0; i &lt; checkedListBox1.Items.Count; i++)
                      {
                          checkedListBox1.SetItemChecked(i, false);//First uncheck the old value!
                          //
                          for (int x = 0; x &lt; listBox1.Items.Count; x++)
                          {
                              if (checkedListBox1.Items[i].ToString() == listBox1.Items[x].ToString())
                              {
                                  //Check only if they match! 
                                  checkedListBox1.SetItemChecked(i, true);
                              }
                          }
                      }
                  }
              }
          }
  
          private void button2_Click(object sender, EventArgs e)
          {
              listBox1.Items.Clear();
          }
      }
  }
  </code></pre>