簡體   English   中英

如何將項目從一個checkedListBox移動到另一個checkedListBox

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM