C#程序设计实验报告WindowsForm程序设计实验(实验2第四题)附源码

课程名称 C#程序设计

实验名称 实验二

肆、第四题

一、实验题目

创建项目,设计一个购物车程序,从左侧选择项目添加到右侧,单击“确定”按钮后显示消息窗口,如下图所示。
在这里插入图片描述

二、实验要求

具体要求:
(1) 按照图示排列相应的控件,控件名称自定义;
(2) 界面中左侧和右侧分别是两个ListBox控件,控件名称自定义;
(3) 6个button按钮的功能从上到下依次为:【从左侧到右侧添加一项】、【从右侧移除一项】、【从左侧到右侧添加全部】、【从右侧移除全部】以及【确定】和【取消】
(4) 当单击【确定】按钮后,弹出对话框,如上图所示。
所需控件为:
1个Label,属性Text为购物车;2个ListBox(第一个ListBox属性Items中写C语言、C#、网络、Linux、Java、离散);6个Button,属性Text分别为>、<、》、《、确定、取消

三、实验代码以及执行结果

1、Form1.cs:
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 第四题
{
    public partial class Form1 : Form
    {
        List<string> itemValues;
        int count;
        public Form1()
        {
            InitializeComponent();
        }

        private void LeftToRight_Click(object sender, EventArgs e)//将左边选中的文本项移到右边
        {
            count = leftListBox.SelectedItems.Count;   //将左边选中的文本项数目
            itemValues = new List<string>();           //选中文本集合
            if (count != 0)
            {
                for (int i = 0; i < count; i++)        //添加文本集合,并往右边添加文本项
                {
                    itemValues.Add(leftListBox.SelectedItems[i].ToString());
                    rightListBox.Items.Add(leftListBox.SelectedItems[i].ToString());
                }
                foreach (string item in itemValues)     //删除左边已选中的文本项
                {
                    leftListBox.Items.Remove(item);     
                }   
            }
        }

        private void deleteRight_Click(object sender, EventArgs e)
        {
            count = leftListBox.SelectedItems.Count;   //将左边选中的文本项数目
            itemValues = new List<string>();           //选中文本集合
            if (count != 0)
            {
                for (int i = 0; i < count; i++)        //添加文本集合
                {
                    itemValues.Add(rightListBox.SelectedItems[i].ToString());
                }
                foreach (string item in itemValues)     //删除右边已选中的文本项
                {
                    rightListBox.Items.Remove(item);    
                }
            }
        }

        private void LeftToRightAll_Click(object sender, EventArgs e)
        {
            itemValues = new List<string>();                   //选中文本集合
            for (int i = 0; i < leftListBox.Items.Count; i++)  //添加文本集合
            {
                itemValues.Add(leftListBox.Items[i].ToString());
            }
            foreach (string item in itemValues)                //将左边所有的文本项移到右边
            {
                rightListBox.Items.Add(item);
                leftListBox.Items.Remove(item);
            }
        }

        private void deleteRightAll_Click(object sender, EventArgs e)
        {
            itemValues = new List<string>();                    //选中文本集合
            for (int i = 0; i < rightListBox.Items.Count; i++)  //添加文本集合
            {
                itemValues.Add(rightListBox.Items[i].ToString());
            }
            foreach (string item in itemValues)                 //删除右边所有的文本项
            {
                rightListBox.Items.Remove(item);               
            }
        }

        private void showSelected_Click(object sender, EventArgs e)    //展示已选中的文本项信息
        {
            String selectedItemText = "";                              //选中的文本项字符串
            for (int i = 0; i < leftListBox.SelectedItems.Count; i++)  //将左侧选中的文本项添加到字符串中
            {
                if (i == leftListBox.SelectedItems.Count - 1)//如果是最后一个文本项则不在后面加逗号
                {
                    selectedItemText += leftListBox.SelectedItems[i].ToString();
                }
                else
                {
                    selectedItemText += leftListBox.SelectedItems[i].ToString() + ",";
                }
            }
            for (int i = 0; i < rightListBox.SelectedItems.Count; i++)//将右侧选中的文本项添加到字符串中
            {
                if (i == rightListBox.SelectedItems.Count - 1)//如果是最后一个文本项则不在后面加逗号
                {
                    selectedItemText += rightListBox.SelectedItems[i].ToString();
                }
                else
                {
                    selectedItemText += rightListBox.SelectedItems[i].ToString() + ",";
                }
            }
            MessageBox.Show(selectedItemText, "您选择的是:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
        }

        private void cancelSelected_Click(object sender, EventArgs e)   //关闭窗口
        {
            this.Close();
        }
    }
}


2、执行结果:

在这里插入图片描述

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

四、实验总结

通过这次实验,我掌握了ListBox的基本用法,通过调用rightListBox.SelectedItems 来获取listbox中已选中的所有节点,并通过调用rightListBox.Items.Add(item)和leftListBox.Items.Remove(item)实现节点的添加与删除,参数可以是节点的text属性也可以是一个item对象。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

别卷了,球球了。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值