⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 Visual C#2005程序设计教程
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WinAppRandom
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
        private System.Windows.Forms.Button btnOK;
        private Button btnInit;
        private Label lblAnswer;
        private Label label2;
        private Label label1;
        private TextBox txtResult;
        private TextBox txtNum2;
        private TextBox txtNum1;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
            this.btnOK = new System.Windows.Forms.Button();
            this.btnInit = new System.Windows.Forms.Button();
            this.lblAnswer = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.txtResult = new System.Windows.Forms.TextBox();
            this.txtNum2 = new System.Windows.Forms.TextBox();
            this.txtNum1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // btnOK
            // 
            this.btnOK.Location = new System.Drawing.Point(76, 144);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new System.Drawing.Size(88, 32);
            this.btnOK.TabIndex = 0;
            this.btnOK.Text = "确定";
            this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
            // 
            // btnInit
            // 
            this.btnInit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnInit.Location = new System.Drawing.Point(236, 144);
            this.btnInit.Name = "btnInit";
            this.btnInit.Size = new System.Drawing.Size(88, 32);
            this.btnInit.TabIndex = 4;
            this.btnInit.Text = "出题";
            this.btnInit.Click += new System.EventHandler(this.button2_Click);
            // 
            // lblAnswer
            // 
            this.lblAnswer.Location = new System.Drawing.Point(220, 88);
            this.lblAnswer.Name = "lblAnswer";
            this.lblAnswer.Size = new System.Drawing.Size(144, 23);
            this.lblAnswer.TabIndex = 13;
            this.lblAnswer.Text = "正确答案为:";
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(244, 40);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(32, 23);
            this.label2.TabIndex = 12;
            this.label2.Text = "=";
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(124, 40);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(32, 23);
            this.label1.TabIndex = 11;
            this.label1.Text = "+";
            // 
            // txtResult
            // 
            this.txtResult.Location = new System.Drawing.Point(284, 40);
            this.txtResult.Name = "txtResult";
            this.txtResult.Size = new System.Drawing.Size(82, 25);
            this.txtResult.TabIndex = 10;
            // 
            // txtNum2
            // 
            this.txtNum2.Location = new System.Drawing.Point(156, 40);
            this.txtNum2.Name = "txtNum2";
            this.txtNum2.ReadOnly = true;
            this.txtNum2.Size = new System.Drawing.Size(80, 25);
            this.txtNum2.TabIndex = 9;
            // 
            // txtNum1
            // 
            this.txtNum1.Location = new System.Drawing.Point(34, 40);
            this.txtNum1.Name = "txtNum1";
            this.txtNum1.ReadOnly = true;
            this.txtNum1.Size = new System.Drawing.Size(82, 25);
            this.txtNum1.TabIndex = 8;
            // 
            // Form1
            // 
            this.AcceptButton = this.btnOK;
            this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
            this.CancelButton = this.btnInit;
            this.ClientSize = new System.Drawing.Size(400, 200);
            this.Controls.Add(this.lblAnswer);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.txtResult);
            this.Controls.Add(this.txtNum2);
            this.Controls.Add(this.txtNum1);
            this.Controls.Add(this.btnInit);
            this.Controls.Add(this.btnOK);
            this.Name = "Form1";
            this.Text = "简单加法练习程序";
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			Random rn=new Random( );    //  声明随机数对象
			txtNum1.Text=rn.Next(100).ToString( );    // 生成一个0~100的随机数并赋值给txtNum1的Text属性
			txtNum2.Text=rn.Next(100).ToString( );
			txtResult.Text="";				// 清空计算结果文本框
			txtResult.Focus( );				// 使计算结果文本框得到焦点
			lblAnswer.Text = "正确答案为:";	// 初始化显示正确答案标签
			btnInit.Enabled=false;          // 出题按钮不可用
			//string s="abCdEfG";
			//s.ToUpper( );
			//textBox3.Text=s.ToLower( );
		}

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			int add=int.Parse(txtNum1.Text) + int.Parse(txtNum2.Text);
			lblAnswer.Text = "正确答案为:" + add.ToString( );    // 显示正确答案
			btnInit.Enabled=true;    // 出题按钮可用
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -