- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace 课程选择
- {
- public partial class Form1 : Form
- {
- private int totalHours = 0;
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- Course[] courses = new Course[8] { new Course("大学英语", 50), new Course("高等数学", 45), new Course("计算机基础", 30), new Course("C语言", 40), new Course("大学物理", 24), new Course("电子技术", 30), new Course("数据库设计", 45), new Course("编译原理", 55) };
- for (int i = 0; i < 8; i++)
- {
- comboBox1.Items.Add(courses[i]);
- }
- textBox1.Text = "0";
- }
- private void btnAdd_Click(object sender, EventArgs e)
- {
- if (comboBox1.SelectedIndex != -1)//判断是否选择了课程
- {
- Course c1 = (Course)comboBox1.SelectedItem;
- if (!listBox1.Items.Contains(c1))//如果listBox1中不包括选择的课程
- {
- listBox1.Items.Add(c1);//添加到listBox1中
- totalHours += c1.hours;//学时随之增加
- textBox1.Text = totalHours.ToString();//显示到textBox1中
- }
- }
- }
- private void btnDelete_Click(object sender, EventArgs e)
- {
- if (listBox1.SelectedIndex != -1)//判断是否选择了课程
- {
- Course c1 = (Course)listBox1.SelectedItem;
- listBox1.Items.Remove(c1);//从listBox1中清除
- totalHours -= c1.hours;//学时随之减少
- textBox1.Text = totalHours.ToString();
- }
- }
- }
- public class Course
- {
- public string name;//课程名
- public int hours;//学时
- public Course(string name, int hours)
- {
- this.name = name;
- this.hours = hours;
- }
- public override string ToString()
- {
- return name;
- }
- }
- }