【C#】编写计算机选课程序

文章目录


一、引言

在这篇文章中,我将介绍如何开发一个简单的计算机选课程序。这个程序将允许学生根据自己的需求选择不同等级的课程,并即时显示他们的选课情况。我们将使用C#语言和Windows Forms来构建这个应用程序。

二、程序概述

我们的选课程序将包含三个等级的课程:基础课、语言课和应用课。基础课和语言课为限选课程,而应用课为任选课程。学生每选一门课程,选课情况将立即显示在多行文本框中。

三、程序设计

四、 界面设计

程序界面将包含三个部分:基础课选择、语言课选择和应用课选择。基础课和语言课将使用单选按钮(RadioButton)来选择,而应用课将使用复选框(CheckBox)来选择。


五、代码实现

csharp 复制代码
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 _6_1_
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string first = "";
        string second = "";
        string third = "";

        // 检查基础课选择
        private void checkFirst() {
            if (radioButton1.Checked)
                first = radioButton1.Text;
            else if (radioButton2.Checked)
                first = radioButton2.Text;
        }

        // 检查语言课选择
        private void checkSecond()
        {
            if (radioButton4.Checked)
                second = radioButton4.Text;
            else if (radioButton3.Checked)
                second = radioButton3.Text;
            else if (radioButton6.Checked)
                second = radioButton6.Text;
            else if (radioButton5.Checked)
                second = radioButton5.Text;
            else if (radioButton7.Checked)
                second = radioButton7.Text;
        }

        // 检查应用课选择
        private void checkThird() 
        {
            third = "";
            if (checkBox1.Checked)
                third += " " + checkBox1.Text + " ";
            if (checkBox2.Checked)
                third += " " + checkBox2.Text + " ";
            if (checkBox3.Checked)
                third += " " + checkBox3.Text + " ";
            if (checkBox4.Checked)
                third += " " + checkBox4.Text + " ";
            if (checkBox5.Checked)
                third += " " + checkBox5.Text + " ";
        }

        // 显示选课结果
        private void showMsg() {
            textBox1.Text = "您的选课结果如下:\r\n";
            if (!first.Equals(""))
                textBox1.Text += "基础:" + first + "\r\n";
            if (!second.Equals(""))
                textBox1.Text += "语言:" + second + "\r\n";
            if (!third.Equals(""))
                textBox1.Text += "应用:" + third + "\r\n";
        }

        // 事件处理
        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            checkFirst();
            showMsg();
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            checkSecond();
            showMsg();
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            checkThird();
            showMsg();
        }
    }
}

六、为每一个选项添加事件

每一个选项都要选择右边菜单栏中的check事件


七、事件处理

  • radioButton4_CheckedChanged:当语言课的单选按钮状态改变时,调用checkFirstshowMsg方法来更新选课结果。
  • checkBox4_CheckedChanged:当应用课的复选框状态改变时,调用checkThirdshowMsg方法来更新选课结果。

八、完成展示

相关推荐
boonya15 小时前
Java JVM核心原理与面试题解析
java·开发语言·jvm
魔都吴所谓15 小时前
【Python】根据开始时间、结束时间计算中间时间
开发语言·windows·python
Sammyyyyy16 小时前
Node.js 做 Web 后端优势为什么这么大?
开发语言·前端·javascript·后端·node.js·servbay
27^×16 小时前
Java 内存模型与垃圾回收机制详解
java·开发语言
max50060017 小时前
本地部署开源数据生成器项目实战指南
开发语言·人工智能·python·深度学习·算法·开源
q5673152317 小时前
手把手教你用Go打造带可视化的网络爬虫
开发语言·爬虫·信息可视化·golang
Bling_Bling_117 小时前
面试常考:js中 Map和 Object 的区别
开发语言·前端·javascript
程序喵大人17 小时前
写C++十年,我现在怎么设计类和模块?(附真实项目结构)
开发语言·c++·类和模板
黄焖鸡能干四碗18 小时前
信息系统安全保护措施文件方案
大数据·开发语言·人工智能·web安全·制造