listbox1添加到listbox2中处理

来源:互联网 发布:redis数据加密 编辑:程序博客网 时间:2024/06/15 23:38
  for (int i = 0; i < this.ListBox1.Items.Count; i++)
        {
            if (this.ListBox1.Items[i].Selected == true)
            {
                if (this.ListBox2.Items.Count == 0) { this.ListBox2.Items.Add(ListBox1.Items[i]); } // listbox2中没有数据
                else
                {
                    for (int j = 0; j < this.ListBox2.Items.Count; j++)
                    {
                        //listbox中是否已存在
                        if (ListBox2.Items[j].Text.ToString().Equals(ListBox1.Items[i].Text.ToString()))
                        {
                            break; //跳出本次循环
                        }
                        if (j == this.ListBox2.Items.Count - 1)  //不存在
                        {
                           this.ListBox2.Items.Add(this.ListBox1.Items[i]);  //选中项写入listbox2中
                          this.ListBox2.Items[ListBox2.Items.Count - 1].Selected = false;
                        }
                    }
                }
            }
        }
原创粉丝点击