c#如果匹配列表框中的项目,如何设置已检查列表框中项目的检查状态

时间:2017-03-20 13:16:22

标签: c# winforms

enter image description here

以下是我一直在尝试做的事情:

如果我向listbox1添加成员,变量将存储这些项目并使用 stringbuilder 将它们转换为单个字符串,其中字符串将显示为listbox2上的项目。我已经开始工作了。

我想将listbox2上的所选项目显示为listbox1,作为我已经工作的分隔字符串/项目,但我还要检查checklistbox1上与listbox1上的项目匹配的所有项目,其中我有麻烦,因为只有1项在checklistbox1上使用for循环检查。

希望有人可以帮我解决这个问题。

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 < 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 < tokens.Count(); y++)
            {
                listBox1.Items.Add(tokens[y]);

                //loop code for checking matching items between listbox and checkedlistbox
                for (int item = 0; item < listBox1.Items.Count; item++)
                {
                    for (int i = 0; i < checkedListBox1.Items.Count; i++)
                    {
                        checkedListBox1.SetItemChecked(i, false);//First uncheck the old value!
                        //
                        for (int x = 0; x < 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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果我理解你要做什么,有几个问题:

  1. StringBuilder.AppendLineNewLine嵌入到您从listbox1中分割出所选项目的项目中,因此比较将永远不会匹配。
  2. 您取消勾选循环内的CheckedListBox项并清除您已找到的匹配项。
  3. 请改为尝试:

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            //Uncheck the checkedListBox items only once.
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                checkedListBox1.SetItemChecked(i, false);
            }
            s = listBox2.SelectedItem.ToString();
            //Remove all the NewLines added by StringBuilder.AppendLine
            s = s.Replace(Environment.NewLine, "");
            //Split and ensure no empty elements
            string[] tokens = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    
            //Loop through tokens
            for (int y = 0; y < tokens.Length; y++)
            {
                //Add token to listBox1
                listBox1.Items.Add(tokens[y]);
    
                //loop through checkedListBox1.Items for each token
                for (int i = 0; i < checkedListBox1.Items.Count; i++)
                {
                    if (checkedListBox1.Items[i].ToString() == tokens[y])
                    {
                        //Check only if they match! 
                        checkedListBox1.SetItemChecked(i, true);
                    }
                }
            }
        }
    
相关问题