在ListBox控件间交换数据

实现效果:

  

知识运用:

  ListBox控件的SelectedItem属性  //获取或设置ListBox控件中当前选定的数据项

  public Object SelectedItem{ get;set; }

  和Items集合的Clear方法  //用于从数据项集合中移除所有的数据项

  publIc virtual void Clear()

实现代码:

        private void button3_Click(object sender, EventArgs e)  //全部添加到选择的项中
        {
            for (int i = 0; i < listBox2.Items.Count; i++)
            {
                listBox2.SelectedIndex = i;                     //按索引选中项
                listBox1.Items.Add(listBox2.SelectedItem);      //添加新项
            }
            listBox2.Items.Clear();                             //清空项
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try { listBox2.Items.Add(listBox1.SelectedItem); }
            catch { MessageBox.Show("你还没有选择"); } 
        }

猜你喜欢

转载自www.cnblogs.com/feiyucha/p/10161061.html