简体   繁体   English

如何将项目从一个checkedListBox移动到另一个checkedListBox

[英]How to move items from a checkedListBox to another checkedListBox

I made a program which adds checkedListBox.Items from a text written in a TextBox . 我编写了一个程序,该程序从TextBox编写的文本中添加了checkedListBox.Items Regarding this, to make everything more esthetic , I made a rule so that if the number of Items added in CheckedListBox1 is bigger than a number I set, it will go to a second CheckedListBox and so on. 关于这一点,为了使所有内容更美观,我制定了一条规则,以便如果CheckedListBox1添加的Items数量大于我设置的数量,它将转到第二个CheckedListBox ,依此类推。

I can also save my Entries in a .txt file so I have easy access to my previous references. 我还可以将条目保存为.txt文件,以便可以轻松访问以前的参考。 So naturally I also made a Load References which ,obviously, load the file I saved. 因此,很自然地,我还创建了一个“ Load References ,显然可以加载我保存的文件。

Anyhow, my dillemma is the following : When I press the Load References button it loads ALL the references (Lines) in the text into the first checkedListBox . 无论如何,我的方法如下:当我按下“ Load References按钮时,它将文本中的所有引用(行)加载到第一个checkedListBox I want it to respect the previous law. 我希望它尊重以前的法律。 If I click Load References I want that if there are more than, lets say, 10 entries, all the other ones will go into the other checkedListBox ,by consequence, if the limit number is passed from the second checkedListBox the rest will go into the third one and so on. 如果我单击“ Load References ,则希望有10个以上的条目,所有其他条目都将进入另一个checkedListBox ,因此,如果从第二个checkedListBox传递了限制数,则其余checkedListBox将进入第三等。

I have searched StackOverflow and the Web for several solutions ,some of the more relevant ones : 我在StackOverflow和Web上搜索了几种解决方案,其中一些更为相关:

First found link semi-regarding the subject 首次找到有关该主题的链接

Second found link 第二次找到链接

So to not get it wrong I will state that I want to have all the entries that pass the limit be MOVED to another checkedlistBox ,not copied like the links would suggest. 为了避免出错,我将声明我希望将所有通过限制的条目移动到另一个checkedlistBox ,而不是像链接所建议的那样进行复制。

This is the Line of code for my Load Reference button : 这是我的“ 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)); 
}

Also I tried several methods but this one seemed to be the closest ,even though I got almost no satisfactory result : 我也尝试了几种方法,但是尽管我几乎没有令人满意的结果,但是这似乎是最接近的一种:

var i = checkedListBox1.Items.Count;
if (i >= 10)
   checkedListBox2.Items.Insert(0, checkedListBox1.Items);

Regarding this line of code : It does get an entry send into the second checkedList Box it is just that the entry is called (Collection) and has nothing to do with my references. 关于这一行代码:它确实将条目发送到第二个checkedList Box ,只是该条目被称为(Collection) ,与我的引用无关。

I hope I made myself clear and thank you for support! 希望我能说清楚,谢谢您的支持!

UPDATE 更新

The marked answer works perfectly for this kind of program. 标记的答案非常适合此类程序。 As I have not found anything similar I believe this is most likely the best way to implement the separation of text lines into different checkedListBoxes. 因为我还没有找到类似的东西,所以我相信这很可能是将文本行分离到不同的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]);
}

Set a limit, and maybe a multiplier to control the checkedList the data will be added to. 设置一个限制,并可能设置一个乘数来控制将要添加数据的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;
    }
}

As big as your text file gets, adding data to a checkedListBox just requires you to add a new line to the switch statement. 与文本文件一样大,将数据添加到checkedListBox仅需要您在switch语句中添加新行。

Well, my head is stuck in wpf land, so I would just bind it to a list of lists, in an itemscontrol, or something similar. 好吧,我的头被卡在wpf土地上,所以我将其绑定到列表列表,itemscontrol或类似的东西中。 Reading back, of course, it appears you are using winforms, so this may not be applicable...but i'll post anyways, because it can still be done this way using the WinForms DataRepeater control. 回读,当然,看来您正在使用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++;
    }
}

Then bind the mainList to the control and configure your ItemTemplate. 然后将mainList绑定到控件并配置ItemTemplate。

There was lots of info on binding to the DataRepeater, but here's one link: https://msdn.microsoft.com/en-us/library/cc488279.aspx 关于绑定到DataRepeater的信息很多,但是这里有一个链接: https : //msdn.microsoft.com/zh-cn/library/cc488279.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM