📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Example_1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtInput;
private System.Windows.Forms.Label lblInfo;
private System.Windows.Forms.RadioButton radRange1;
private System.Windows.Forms.Label lblRange;
private System.Windows.Forms.RadioButton radRange2;
private System.Windows.Forms.Button btnCheck;
private System.Windows.Forms.Label lblTrials;
private System.Windows.Forms.Label lblResult;
private int randomNumber;
Random objRandom = new Random();
private System.Windows.Forms.Button btnQuit;
private System.Windows.Forms.ProgressBar pBar;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.txtInput = new System.Windows.Forms.TextBox();
this.lblInfo = new System.Windows.Forms.Label();
this.radRange1 = new System.Windows.Forms.RadioButton();
this.lblRange = new System.Windows.Forms.Label();
this.radRange2 = new System.Windows.Forms.RadioButton();
this.btnCheck = new System.Windows.Forms.Button();
this.pBar = new System.Windows.Forms.ProgressBar();
this.lblTrials = new System.Windows.Forms.Label();
this.lblResult = new System.Windows.Forms.Label();
this.btnQuit = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// txtInput
//
this.txtInput.Location = new System.Drawing.Point(19, 112);
this.txtInput.Name = "txtInput";
this.txtInput.Size = new System.Drawing.Size(120, 21);
this.txtInput.TabIndex = 0;
this.txtInput.Text = "";
//
// lblInfo
//
this.lblInfo.Location = new System.Drawing.Point(19, 78);
this.lblInfo.Name = "lblInfo";
this.lblInfo.Size = new System.Drawing.Size(346, 25);
this.lblInfo.TabIndex = 1;
//
// radRange1
//
this.radRange1.Location = new System.Drawing.Point(19, 43);
this.radRange1.Name = "radRange1";
this.radRange1.Size = new System.Drawing.Size(77, 26);
this.radRange1.TabIndex = 3;
this.radRange1.Text = "1 - 50";
this.radRange1.Click += new System.EventHandler(this.radRange1_Click);
//
// lblRange
//
this.lblRange.Location = new System.Drawing.Point(19, 9);
this.lblRange.Name = "lblRange";
this.lblRange.Size = new System.Drawing.Size(346, 24);
this.lblRange.TabIndex = 4;
this.lblRange.Text = "请选择生成随机数字的范围:";
//
// radRange2
//
this.radRange2.Location = new System.Drawing.Point(106, 43);
this.radRange2.Name = "radRange2";
this.radRange2.Size = new System.Drawing.Size(76, 26);
this.radRange2.TabIndex = 5;
this.radRange2.Text = "1 - 100";
this.radRange2.Click += new System.EventHandler(this.radRange2_Click);
//
// btnCheck
//
this.btnCheck.Location = new System.Drawing.Point(173, 241);
this.btnCheck.Name = "btnCheck";
this.btnCheck.Size = new System.Drawing.Size(90, 25);
this.btnCheck.TabIndex = 6;
this.btnCheck.Text = "检查(&C)";
this.btnCheck.Click += new System.EventHandler(this.btnCheck_Click);
//
// pBar
//
this.pBar.Location = new System.Drawing.Point(19, 181);
this.pBar.Name = "pBar";
this.pBar.Size = new System.Drawing.Size(346, 25);
this.pBar.Step = 20;
this.pBar.TabIndex = 7;
//
// lblTrials
//
this.lblTrials.Location = new System.Drawing.Point(19, 155);
this.lblTrials.Name = "lblTrials";
this.lblTrials.Size = new System.Drawing.Size(77, 17);
this.lblTrials.TabIndex = 8;
this.lblTrials.Text = "尝试次数:";
//
// lblResult
//
this.lblResult.Location = new System.Drawing.Point(163, 112);
this.lblResult.Name = "lblResult";
this.lblResult.Size = new System.Drawing.Size(135, 26);
this.lblResult.TabIndex = 9;
//
// btnQuit
//
this.btnQuit.Location = new System.Drawing.Point(275, 241);
this.btnQuit.Name = "btnQuit";
this.btnQuit.Size = new System.Drawing.Size(90, 25);
this.btnQuit.TabIndex = 10;
this.btnQuit.Text = "退出(&Q)";
this.btnQuit.Click += new System.EventHandler(this.btnQuit_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(384, 272);
this.Controls.Add(this.btnQuit);
this.Controls.Add(this.lblResult);
this.Controls.Add(this.lblTrials);
this.Controls.Add(this.pBar);
this.Controls.Add(this.btnCheck);
this.Controls.Add(this.radRange2);
this.Controls.Add(this.lblRange);
this.Controls.Add(this.radRange1);
this.Controls.Add(this.lblInfo);
this.Controls.Add(this.txtInput);
this.Name = "Form1";
this.Text = "猜数字";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
// This event checks the input number against
// the random number generated and display an appropriate message.
private void btnCheck_Click(object sender, System.EventArgs e)
{
if(int.Parse(txtInput.Text) < randomNumber)
lblResult.Text = "太小";
else if(int.Parse(txtInput.Text) > randomNumber)
lblResult.Text = "太大";
else
{
MessageBox.Show("正确");
Application.Exit();
}
// Increment the progress bar after every trial by 20
pBar.Increment(20);
// If 5 trials are taken then exit the application
if(pBar.Value == 100)
{
MessageBox.Show("您已经用完五次尝试机会");
Application.Exit();
}
}
// Quit the application
private void btnQuit_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
// If radio button offering range 1-50 is selected
// then generate random number between 1-50
private void radRange1_Click(object sender, System.EventArgs e)
{
lblInfo.Text = "数字介于 1 至 50 之间。请猜出该数字";
randomNumber = objRandom.Next(1, 50);
txtInput.Enabled = true;
}
// If radio button offering range 1-100 is selected
// then generate random number between 1-100
private void radRange2_Click(object sender, System.EventArgs e)
{
lblInfo.Text = "数字介于 1 至 100 之间。请猜出该数字";
randomNumber = objRandom.Next(1, 100);
txtInput.Enabled = true;
}
// Disable txtInput on form load
private void Form1_Load(object sender, System.EventArgs e)
{
txtInput.Enabled = false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -