C# c如果与列表框中的项目匹配,如何设置选中列表框中项目的选中状态
以下是我一直试图做的: 如果我将成员添加到listbox1,一个变量将存储这些项,并使用stringbuilder将它们转换为单个字符串,其中字符串将作为一个项显示在listbox2上。我已经开始工作了 我想将listbox2到listbox1上的所选项目显示为已使用的分隔字符串/项目,但我还想检查checklistbox1上与listbox1上的项目匹配的所有项目,因为使用for循环在checklistbox1上只检查了1个项目,所以我在其中遇到了问题 希望有人能在这件事上帮我C# c如果与列表框中的项目匹配,如何设置选中列表框中项目的选中状态,c#,winforms,C#,Winforms,以下是我一直试图做的: 如果我将成员添加到listbox1,一个变量将存储这些项,并使用stringbuilder将它们转换为单个字符串,其中字符串将作为一个项显示在listbox2上。我已经开始工作了 我想将listbox2到listbox1上的所选项目显示为已使用的分隔字符串/项目,但我还想检查checklistbox1上与listbox1上的项目匹配的所有项目,因为使用for循环在checklistbox1上只检查了1个项目,所以我在其中遇到了问题 希望有人能在这件事上帮我 using S
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();
}
}
}
如果我理解您的意图,那么有几个问题: StringBuilder.AppendLine将新行嵌入要从listbox1中所选项目中拆分出来的项目中,因此比较将永远不会匹配。 您取消选中的CheckedListBox项在循环中太远,并且清除了已找到的匹配项。 请尝试以下方法:
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);
}
}
}
}
你能发布你当前的代码吗?很难确定你的程序做什么和应该做什么。使用清晰映射到表单控件的名称,指示项目是手动输入到CheckedListBox中的,等等。一般来说,尝试发布可以编译和运行的代码,而无需任何推测。谢谢,这正是我所需要的。如果你能向我推荐一些关于你如何学会用c编写代码的书籍或任何学习资料,我也会很感激,我可能会学到一些我可能会错过的新东西。不管怎么说,非常感谢你。有很多编程书籍,但对我来说,它总是第一个任务-我如何使计算机做一些事情。然后我就具体的任务进行工作和研究。基于您遇到的困难,我建议您将重点放在使用调试器和理解流控制上。使用调试器逐步检查代码将很快发现这两个问题。一旦你知道具体发生了什么,你就可以着手纠正它。注意,谢谢!。