移动从KeyValuePair列表中填充的列表框中的项目。的WinForms

时间:2014-03-31 15:54:39

标签: c# winforms sorting listbox

enter image description here

我使用以下代码从列表中填充列表框:

     uList.Add(new KeyValuePair<int, string>(item.Id, item.Name));
     listBoxHome.DataSource = uList;
     listBoxHome.DisplayMember = "Value";
     listBoxHome.ValueMember = "Key";

然后我以新的对话框形式(Subtitiution)复制所有项目,并填写checkedListBox1

     private void button6_Click(object sender, EventArgs e)
    {
        Sub sub = new Sub();
        for (int i = 0; i < listBoxHome.Items.Count; i++)
        {
            sub.checkedListBox1.Items.Add(listBoxHome.Items[i].ToString());
        }                        
        this.Opacity = 0.7;     
        sub.ShowDialog();
    }

enter image description here 然后,我将所选项目的选定索引放入列表中。 我想选择5个项目,然后将它们放在listbox的前5个位置。游戏是第一个表单。我尝试以下代码,但它给了我错误。有什么想法吗?

   private void button2_Click(object sender, EventArgs e)
    {
        List<int> indexes = new List<int>();
        foreach (int indexChecked in checkedListBox1.CheckedIndices)
        {
            indexes.Add(indexChecked);
        }
        Game game = new Game();
        foreach (int PlayingInd in indexes) // Loop through List with foreach
        {
            game.listBoxHome.Items.Insert(0, game.listBoxHome.Items[PlayingInd]);
            game.listBoxHome.Items.RemoveAt(PlayingInd);
        }
        this.Close();
    }

0 个答案:

没有答案