当前位置: 首页 > news >正文

实验三 Windows窗体的设计及常用控件(1)

前言
👏作者简介:我是笑霸final,一名热爱技术的在校学生。
📝个人主页:个人主页1 || 笑霸final的主页2
📕系列专栏:《作业专栏》
📧如果文章知识点有错误的地方,请指正!和大家一起学习,一起进步👀
🔥如果感觉博主的文章还不错的话,👍点赞👍 + 👀关注👀 + 🤏收藏🤏

在这里插入图片描述

目录

  • 一、实验目的
  • 二、实验任务
    • 第一题
    • 第二题
    • 第三题
    • 第四题

一、实验目的

1.掌握窗体应用程序的创建及其构成。
2.掌握控件的添加、控件的布局调整以及属性的设置。
3.掌握命令按钮、文本框、标签控件、单选按钮、复选框的使用。
4.掌握消息对话框的使用。

二、实验任务

第一题

1.创建Windows窗体应用程序,设计一个用户登录窗口,如图3.1所示,要求输入用户名和密码(设用户名为admin,密码为123),输入正确后利用消息框显示如图3.2所示欢迎信息“登录成功,欢迎使用本系统”,点击消息框中“确定”按钮后,打开另一个窗体,如图3.3所示;输入不正确显示如图3.4所示消息框,若错误3次则程序结束。
要求:
(1)用户名或密码均不能为空,否则显示消息框如图3.5所示。
(2) 除了点击“登录”按钮实现登录验证外,当在密码文本框中输入密码后直接按Enter键也能实现登录效果。
在这里插入图片描述
在这里插入图片描述

源代码:

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 Experiment_3
{
    public partial class Form1 : Form
    {
        int RrrorCount = 0;//用来统计 密码和用户名输入错误的次数
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            
        }
        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                textBox1.Focus(); //当在文本框1中检查到回车键时,直接将焦点转入TextBox2
            }
        }
         


        private void button1_Click(object sender, EventArgs e)
        {
            String user=textBox1.Text;
            String password =textBox2.Text;
            if(user.Equals("")|| password.Equals(""))
            {//当密码和用户名为空时(必须同时都有值才不会进入此判断)
                MessageBox.Show("密码和用户名均不能为空", "登陆", MessageBoxButtons.OK);
            }else if("admin".Equals(user)&& int.Parse(password) == 123)
            {
                //密码和用户名匹配成功时
                MessageBox.Show("登陆成功,欢迎使用本系统", "登陆", MessageBoxButtons.OK);
                Form f2 = new Form2();//创建第二个窗口实例
                f2.Show();//打开第二个窗口
            }
            else if(!"admin".Equals(user) || ! (int.Parse(password) == 123))
            {
                MessageBox.Show("用户名或者密码错误,请重新输入", "登陆", MessageBoxButtons.OK);
                RrrorCount++;
                textBox1.Clear();//清除输入内容
                textBox2.Clear();//清除输入内容
                if (RrrorCount >= 3)
                {//说明已经输入错误三次了程序结束
                    this.Close();
                    return;
                }
            }
        }

       
    }
}

运行结果
在这里插入图片描述

第二题

2.创建Windows窗体应用程序,利用文本框、单选按钮、分组控件、复选框设计界面,如图3.6所示,实现根据选择控件的单击实现实时改变文本框中文字显示效果的功能。在这里插入图片描述

源代码

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Experiment_3_2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void radioButton1_Click(object sender, EventArgs e)
        {
            //几个按钮绑定得共享事件
            RadioButton btn =(RadioButton)sender;
            if (btn.Text.Equals("宋体"))
            {
                //设置宋体 其他不变 下面得else if 同理
                richTextBox1.Font=
                    new Font("宋体", richTextBox1.Font.Size, richTextBox1.Font.Style);
               
            }
            else if (btn.Text.Equals("黑体"))
            {
                richTextBox1.Font =
                   new Font("黑体", richTextBox1.Font.Size, richTextBox1.Font.Style);
            }
            else if (btn.Text.Equals("楷体"))
            {
                richTextBox1.Font =
                   new Font("楷体", richTextBox1.Font.Size, richTextBox1.Font.Style);
            }
            else if (btn.Text.Equals("隶书"))
            {
                richTextBox1.Font =
                   new Font("隶书", richTextBox1.Font.Size, richTextBox1.Font.Style);
            }


        }

        private void radioButton5_Click(object sender, EventArgs e)
        {
            //几个按钮绑定得共享事件

            RadioButton btn = (RadioButton)sender;
//设置字体大小 其他不变 下面得else if 同理

            if (btn.Text.Equals("10"))
            {
                richTextBox1.Font =
                    new Font(richTextBox1.Font.FontFamily,10, richTextBox1.Font.Style);

            }
            else if (btn.Text.Equals("15"))
            {
                richTextBox1.Font =
                   new Font(richTextBox1.Font.FontFamily, 15, richTextBox1.Font.Style);

            }
            else if (btn.Text.Equals("20"))
            {
                richTextBox1.Font =
                   new Font(richTextBox1.Font.FontFamily, 20, richTextBox1.Font.Style);

            }
            else if (btn.Text.Equals("25"))
            {
                richTextBox1.Font =
                   new Font(richTextBox1.Font.FontFamily, 25, richTextBox1.Font.Style);

            }

        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {

        }

        

        private void Form1_Activated(object sender, EventArgs e)
        {

        }

        private void Form1_Click(object sender, EventArgs e)
        {
            
        }

        private void checkBox1_Click(object sender, EventArgs e)
        {
            if (this.checkBox1.Checked)
            {//当 粗体被选中时
                richTextBox1.Font =
                   new Font(richTextBox1.Font.FontFamily, richTextBox1.Font.Size, FontStyle.Bold);
               
            }
            else
            {//当 粗体未被选中时
                richTextBox1.Font =
                   new Font(richTextBox1.Font.FontFamily, richTextBox1.Font.Size, FontStyle.Regular);

                
            }
        }
    }
}

运行结果
在这里插入图片描述

第三题

3.创建Windows窗体应用程序,实现“上网问卷调查”,调查内容有:年龄,每天平均上网时间,上网主要做什么,如图3.7所示。当回答完问题,点击“确定”按钮后,弹出“调查结果”消息框,显示用户的选择,如图3.8所示。在这里插入图片描述

using System;
using System.Windows.Forms;

namespace Experiment_3_3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //创建一些初始化得提示字符串
            string myAge = "你的年龄为:";
            string myTime = "你的上网时间为:";
            string myDo = "你上网主要是:";

            //选择年龄
            if (age1.Checked)//10岁被选中时
            {
                myAge += age1.Text ;
            }
            else if (age2.Checked)//11-17岁被选中
            {
                myAge += age2.Text;
            }
            else if (age3.Checked)//18-40岁被选中
            {
                myAge += age3.Text ;
            }
            else if (age4.Checked)
            {
                myAge +=  age4.Text ;
            }
            else
            {
                myAge += "未选择年龄" ;
            }

            //选择上网时间
            if (time1.Checked)//小于1小时
            {
                myTime += time1.Text ;
            }
            else if (time1.Checked)//1-2小时
            {
                myTime += time2.Text ;
            }
            else if (time3.Checked)//1-3小时
            {
                myTime += time3.Text ;
            }
            else if (time4.Checked)//4小时以上
            {
                myTime += time4.Text ;
            } else
            {
                myTime += "未选择上网时长";
            }

            //上网干啥
            if (checkBox1.Checked)//浏览咨询
            {
                myDo += "浏览咨询、";
            }
            if (checkBox2.Checked)//聊天
            {
                myDo += "聊天、";
            }
            if (checkBox3.Checked)//购物
            {
                myDo += "购物、";
            }
            if (checkBox4.Checked)//看视频
            {
                myDo += "看视频、";
            }
            if (checkBox5.Checked)//收发邮件
            {
                myDo += "收发邮件、";
            }
            if (checkBox6.Checked)
            {
                myDo += "其他、";
            }
            myDo=myDo.Remove(myDo.Length-1, 1);//移除多余得、号
            myDo += "。";//添加句号
            MessageBox.Show(myAge + "\n" + myTime + "\n" + myDo,"调查结果");
        }
    }
}

运行结果
在这里插入图片描述

第四题

4.创建Windows窗体应用程序,完成若干加法计算,初始界面如图3.9所示,加数和被加数均为二位数,程序运行时显示第一道题目,输入答案,点击“确定”按钮后,在标签上显示算式并进行是否正确的判断,同时显示下一道题目,完成若干计算后,单击“结束”按钮后,显示“共完成X题,正确:X,错误:X,得分:X”的信息,如图3.10所示。
提示:点击“确定”后显示算式的标签和点击“结束”后显示最终评价的标签使用二个标签。在这里插入图片描述

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;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Experiment_3_4
{
    public partial class Form1 : Form
    {
        Random ramdom = new Random();//创建随机数对象
        int in1, in2;//随机两数
        int correct=0, error=0;

        private void button1_Click(object sender, EventArgs e)
        {


            //点击确定时

            try
            {
                if (int.Parse(textBox1.Text) == in1 + in2)
                {
                    //正确时
                    correct++;
                    output1.Text += in1 + "+" + in2 + "=" + textBox1.Text + "✔" + '\n';

                }
                else
                {
                    //错误时
                    error++;
                    output1.Text += in1 + "+" + in2 + "=" + textBox1.Text + "✖" + '\n';
                }
            }
            catch(Exception ee)
            {//发生异常
                MessageBox.Show("请输入数字");
                textBox1.Text = "";
                
            }
            
            //输入框清空
            textBox1.Text = "";
            textBox1.Focus();//textBox1重新获取 焦点
            output1.Visible = true;
            //更新数据
            Form1_Activated(null,null);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //结束按钮
            double score = correct*1.0 / (correct + error)*100;
            //显示数据
            output2.Text = "一共完成" + (correct + error)
               + "题" + ",正确" + correct + "题" + ",错误" + error + "题," +
               "得分:" + score.ToString("f2") + "分";
            output2.Visible = true;
        }


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            //窗口激活时发生的时间
            in1 = ramdom.Next(10,100);
            in2 = ramdom.Next(10, 100);
            input1.Text = in1.ToString();
            input2.Text = in2.ToString();
        }
    }
}

运行结果
在这里插入图片描述
在这里插入图片描述

相关文章:

  • 图像的矩(MATLAB源码)
  • 8.4.3 使用3:配置单臂路由实现VLAN间路由
  • 【鸿蒙开发】后台任务
  • 攻防世界---misc---再见李华
  • 物联网配网工具多元化助力腾飞——智能连接,畅享未来
  • STM32的GPIO控制寄存器开发
  • ArcGIS Runtime For Android开发之符号化和图层渲染
  • 【vue+element ui】大屏自适应中el-select下拉内容在低分辨率下显示不全问题解决
  • c# 异常处理
  • 编译opencv gpu版的条件
  • 《TCP/IP详解 卷一》第6章 DHCP
  • Docker - compose常用命令(常规操作顺序)
  • 【计算机毕业设计】java SpringBoot校园大学生志愿者服务系统
  • 【深入理解Kafka系列】第六章 __consumer_offsets(位移主题)
  • 脑机接口科普0008——侵入式与非侵入式
  • Nginx网站服务
  • Python游戏嗷大喵快跑设计
  • nginx负载均衡高可用部署
  • 【附源码】计算机毕业设计SSM怦然心动网上服装商城
  • YOLOv5实现佩戴安全帽检测和识别(含佩戴安全帽数据集+训练代码)
  • 【以太网硬件十六】双绞线有哪些种类?
  • 从零开始配置tensorflow深度学习环境(含cuda以及其他依赖)
  • 模型机的组合逻辑控制器
  • 私域流量和公域流量有何区别?为什么要打造自己的私域流量?
  • Python中的图像调整和裁剪工具
  • 基于单片机的智能照明系统
  • Visual Studio扩展插件
  • 12.MongoDB系列之副本集管理
  • 【初学者入门C语言】之编译预处理(十)
  • 【JavaWeb】之Maven
  • 牛客网专项练习30天Pytnon篇第25天
  • 【RPA前置知识】 整理并总结 Assign 和 AssignT 类