ListBox内容左右移动

 

页面布局如下:

ListBox内容左右移动_第1张图片  

  public Form1()

        {

            InitializeComponent();

            LeftList.Items.Clear();//先是把左边的ListBox的内容清空

            string[] LeftStr = new string[] { "苹果", "香蕉", "荔枝", "雪梨", "西瓜", "哈密瓜", "柠檬" };//声明数组

            foreach (string item in LeftStr)//循环

            LeftList.Items.Add(item);//将数组添加到左边的ListBox中

            RightList.Items.Clear();//移除右边的ListBox中的内容

            string[] RightStr = new string[] { "牛奶", "咖啡", "雪碧", "茶水", "白水" };//声明数组

            foreach (string item in RightStr)RightList.Items.Add(item);//循环新增内容

        }

        private void button4_Click(object sender, EventArgs e)//>按钮的点击事件

        {

            if (LeftList.Items.Count == 0)//判断内容是否选中

            {MessageBox.Show("内容已经是空的了,","系统提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Error);//提示} else

                        {

           while (LeftList.SelectedItems.Count != 0)

            {

              RightList.Items.Add(LeftList.SelectedItems[0]);//将左边选中的添加到右边去

              BottomList.Items.Add(LeftList.SelectedItems[0].ToString() + "被移至右端列表");//下面的ListBox记录下操作

              LeftList.Items.Remove(LeftList.SelectedItems[0]);//将左边选中的移除

                }

            }

        }

        private void button3_Click(object sender, EventArgs e)

        {   foreach (object item in LeftList.Items)//循环左边内容的个数

            RightList.Items.Add(item);//逐个添加到右边

            BottomList.Items.Add("左端列表全部被移至右端列表");//下面ListBox记录下操作

            LeftList.Items.Clear();//清除左边的所有内容

        }

 

        //下面内容雷同(只是方向不同),不做任何解释

        private void button2_Click(object sender, EventArgs e)

        {

            foreach (object item in RightList.Items) LeftList.Items.Add(item);

            BottomList.Items.Add("右端列表全部被移至左端列表");

            RightList.Items.Clear();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            if (RightList.Items.Count == 0)

                return;

            else

            {

                while (RightList.SelectedItems.Count != 0)

                {

                    LeftList.Items.Add(RightList.SelectedItems[0]);

                    BottomList.Items.Add(RightList.SelectedItems[0].ToString() + "被移至左端列表");

                    RightList.Items.Remove(RightList.SelectedItems[0]);

                }

            }

        }

效果图如下:

ListBox内容左右移动_第2张图片

你可能感兴趣的:(ListBox)