答案之书程序改良版本

发布时间:2024年01月22日

答案之书程序改良版本

在这里插入图片描述

C#代码实现

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 WindowsFormsApp1
{
    public partial class Form_AnswerBook : Form
    {
        public Form_AnswerBook()
        {
            InitializeComponent();
        }
        public int maxLength = 4;

        private void Form_AnswerBook_Load(object sender, EventArgs e)
        {
            text_numberorbirthday.MaxLength = maxLength;
        }

        private void text_numberorbirthday_TextChanged(object sender, EventArgs e)
        {
            if (text_numberorbirthday.Text.Length == 0)
            {
                button1.Enabled = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (text_numberorbirthday.Text.Length == 0)
            {
                MessageBox.Show("请输入查询内容", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if(text_numberorbirthday.Text.Length != maxLength) {
                MessageBox.Show("请输入正确内容", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (text_numberorbirthday.Text.Length == 4 && int.TryParse(text_numberorbirthday.Text, out int inputValue))
            {
                Random rand = new Random();
                int randomNumber = rand.Next(1, 801);
                text_answerbookpages.Text = randomNumber.ToString();
            }
            else
            {
                text_answerbookpages.Text = "";
            }
            button1.Enabled = false;
        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            text_numberorbirthday.Text = "";
        }
    }
}

文章来源:https://blog.csdn.net/qq_43001132/article/details/135745248
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。