简体   繁体   中英

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 . 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.

I can also save my Entries in a .txt file so I have easy access to my previous references. So naturally I also made a Load References which ,obviously, load the file I saved.

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 . 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.

I have searched StackOverflow and the Web for several solutions ,some of the more relevant ones :

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.

This is the Line of code for my Load Reference button :

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.

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.

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.

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.

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. 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.

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.

There was lots of info on binding to the DataRepeater, but here's one link: https://msdn.microsoft.com/en-us/library/cc488279.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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