课程名称 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对象。