【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方法来更新选课结果。

八、完成展示

相关推荐
夜雪一千2 分钟前
Python如何使用XPath定位没有特征的元素?无id、无class通用定位技巧
开发语言·python
艾莉丝努力练剑12 分钟前
【QT:解决问题】Qt5Core.dll:无法定位程序输入点
java·开发语言·qt·学习·面试
流浪00119 分钟前
Python 基础语法(一):常量、变量、输入输出与运算符
开发语言·python
光影少年24 分钟前
react离线缓存、图片缓存方案
开发语言·前端·javascript·react native·react.js·缓存·前端框架
SomeB1oody28 分钟前
【RustyML入门】3.7. 循环层
开发语言·后端·机器学习·rust·教程
言乐63 小时前
Python游戏水平测试辅助系统
开发语言·python·游戏·django·pygame
yuhulkjv3358 小时前
Claude表格复制到word不再崩溃,AI导出鸭批量导出+格式无损一键搞定
人工智能·ai·c#·word·ai导出鸭
WWJA王文举10 小时前
I²C通信完整流程详解:START、地址、ACK、数据、Repeated START和STOP一次讲透
c语言·开发语言
zhanghaha131411 小时前
Python进阶教程:6_JSON 数据解析 —— 新手完全指南
开发语言·python·json
qq_4480111612 小时前
C语言中的动态内存分配
c语言·开发语言·php