实现两个ListBox控件间交换数据

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _5_3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "交换数据";
            //添加初始化内容
            listBox1.Items.Add("人工智能");
            listBox1.Items.Add("计算机网络");
            listBox1.Items.Add("软件测试");
            listBox1.Items.Add("移动开发");
            listBox1.Items.Add("Web程序设计");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //for循环移动listBox1中的所有内容
            for(int i = 0; i < listBox1.Items.Count; i++)
            {
                listBox2.Items.Add(listBox1.Items[i]);
            }
            //当全部添加到listBox2后,清空listBox1中的内容
            listBox1.Items.Clear();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)//如果选中图书
            {
                listBox2.Items.Add(listBox1.SelectedItem.ToString());//移动到listBox2
                listBox1.Items.Remove(listBox1.SelectedItem);//清除listBox1中相应的图书
            }
            else//如果没有任何图书被选择,给出提示
            {
                MessageBox.Show("请先选中相应图书!");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < listBox2.Items.Count; i++)//for循环移动listBox2中的所有内容
            {
                listBox1.Items.Add(listBox2.Items[i]);
            }
            listBox2.Items.Clear(); //当全部添加到listBox1后,清空listBox1中的内容
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (listBox2.SelectedIndex != -1)//如果选中图书
            {
                listBox1.Items.Add(listBox2.SelectedItem.ToString());//移动到listBox1
                listBox2.Items.Remove(listBox2.SelectedItem);//清除listBox2中相应的图书
            }
            else//如果没有任何图书被选择,给出提示
            {
                MessageBox.Show("请先选中购物车已有书本!");
            }
        }
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值