答案之书程序改良版本
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/14d635861b2cf6a5a4900b7e7d02fe88.png)
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 = "";
}
}
}