首页 > Windows开发 > 详细

Windows窗体应用程序四(制作随机加法练习器)

时间:2019-05-21 11:25:10      阅读:326      评论:0      收藏:0      [点我收藏+]

【案例】本案例在一个窗体中实现供儿童做的两个一位数相加的练习器。在窗体中能自动产生一道随机的两个一位数相加的练习。

1.添加4个Label控件,1个Button控件,1个TextBox控件。

技术分享图片

2.后台代码如下:

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

        private void Form1_Load(object sender, EventArgs e)
        {
            int n;
            Random randomNumber = new Random();
            n = randomNumber.Next(10);
            lblNum1.Text = n.ToString();
            n = randomNumber.Next(10);
            lblNum2.Text = n.ToString();
        }

        private void txtResult_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(!(Char.IsDigit (e.KeyChar )==true || e.KeyChar == 8))
            {
                e.Handled = true;
            }
            if(e.KeyChar == 13)
            {
                if (txtResult.Text == string.Empty)
                {
                    MessageBox.Show("计算结果不能为空");
                    return;
                }
                else
                {
                    int result = int.Parse(lblNum1.Text) + int.Parse(lblNum2.Text);
                    int myresult = int.Parse(txtResult.Text);
                    if (result == myresult)
                    {
                        MessageBox.Show("恭喜你,答对了");
                        Form1_Load(null, null);
                    }
                    else
                    {
                        MessageBox.Show("对不起,打错了,请再来一次");
                    }
                }
            }            
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

运行结果如下图所示:

技术分享图片

 

Windows窗体应用程序四(制作随机加法练习器)

原文:https://www.cnblogs.com/programme-maker/p/10898606.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!