[英]How to move items from a checkedListBox to another checkedListBox
我编写了一个程序,该程序从TextBox
编写的文本中添加了checkedListBox.Items
。 关于这一点,为了使所有内容更美观,我制定了一条规则,以便如果CheckedListBox1
添加的Items数量大于我设置的数量,它将转到第二个CheckedListBox
,依此类推。
我还可以将条目保存为.txt
文件,以便可以轻松访问以前的参考。 因此,很自然地,我还创建了一个“ Load References
,显然可以加载我保存的文件。
无论如何,我的方法如下:当我按下“ Load References
按钮时,它将文本中的所有引用(行)加载到第一个checkedListBox
。 我希望它尊重以前的法律。 如果我单击“ Load References
,则希望有10个以上的条目,所有其他条目都将进入另一个checkedListBox
,因此,如果从第二个checkedListBox
传递了限制数,则其余checkedListBox
将进入第三等。
我在StackOverflow和Web上搜索了几种解决方案,其中一些更为相关:
为了避免出错,我将声明我希望将所有通过限制的条目移动到另一个checkedlistBox
,而不是像链接所建议的那样进行复制。
这是我的“ Load Reference
按钮的代码行:
private void button8_Click(object sender, EventArgs e)
{
string fileData = File.ReadAllText(@" To-Do References .txt");
checkedListBox1.Items.AddRange(fileData.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));
}
我也尝试了几种方法,但是尽管我几乎没有令人满意的结果,但是这似乎是最接近的一种:
var i = checkedListBox1.Items.Count;
if (i >= 10)
checkedListBox2.Items.Insert(0, checkedListBox1.Items);
关于这一行代码:它确实将条目发送到第二个checkedList Box
,只是该条目被称为(Collection)
,与我的引用无关。
希望我能说清楚,谢谢您的支持!
更新
标记的答案非常适合此类程序。 因为我还没有找到类似的东西,所以我相信这很可能是将文本行分离到不同的checkListListes中的最佳方法。
if you populate listboxes properly there will be no need to move items
private void button8_Click(object sender, EventArgs e)
{
int limit = 10;
string[] fileData = File.ReadAllText(@" To-Do References .txt").Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
// this loop adds items to the 1st list until limit is reached
for(int i =0; i<limit && i<fileData.Length; i++)
checkedListBox1.Items.Add(fileData[i]);
// if there extra items, 2nd loop adds them to list №2
for(int i =limit; i<fileData.Length; i++)
checkedListBox2.Items.Add(fileData[i]);
}
设置一个限制,并可能设置一个乘数来控制将要添加数据的checkedList。
int limit = 10;
int checkList = 1;
string[] fileData = File.ReadAllText(@" To-Do References .txt").Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < fileData.Length; i++)
{
if (i == limit * checkList)
{
checkList++;
}
switch (checkList)
{
case 1: checkedListBox1.Items.Add(fileData[i]); break;
case 2: checkedListBox2.Items.Add(fileData[i]); break;
case 3: checkedListBox3.Items.Add(fileData[i]); break;
}
}
与文本文件一样大,将数据添加到checkedListBox仅需要您在switch语句中添加新行。
好吧,我的头被卡在wpf土地上,所以我将其绑定到列表列表,itemscontrol或类似的东西中。 回读,当然,看来您正在使用winforms,因此这可能不适用...但是我还是会发帖,因为仍然可以使用WinForms DataRepeater控件来完成此操作。
List<List<string>> mainList = new List<List<string>>();
int listIndex = 0;
string[] fileData = File.ReadAllText(@" To-Do References.txt").Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
for(int i = 0; i<=fileData.Length; i++)
{
mainList[listIndex].Add(fileData[i]);
if (i%10 == 0)
{
listIndex++;
}
}
然后将mainList绑定到控件并配置ItemTemplate。
关于绑定到DataRepeater的信息很多,但是这里有一个链接: https : //msdn.microsoft.com/zh-cn/library/cc488279.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.