111,098
社区成员




public partial class Form2 : Form
{
private int current ;//当前产生的随机数
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Random r = new Random();
current = r.Next(1, 10);
}
private void button2_Click(object sender, EventArgs e)
{
int n =Convert.ToInt32(textBox1.Text.ToString().Trim());
if (current == n)
{
MessageBox.Show("恭喜猜中了!");
}
else
{
MessageBox.Show("很遗憾!没有猜中!");
}
label1.Text=current.ToString();
}
}
{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "随机数";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(12, 67);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "猜猜看";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(110, 14);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 2;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(108, 72);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 3;
this.label1.Text = "label1";
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
}
}
protected void Page_Load(object sender, EventArgs e) {
bt2_Click(null, null);
int.TryParse(hf1.Value, out n);
}
protected void Button1_Click(object sender, EventArgs e) {
string k;
k = TextBox1.Text;
if (k == n.ToString()) {
lbshow.Text = "恭喜猜中了!";
Label1.Text = "";
}
else {
lbshow.Text = "很遗憾!没有猜中!";
Label1.Text = "这次的随机为" + n;
}
}
int n;
protected void bt2_Click(object sender, EventArgs e) {
Random r = new Random();
n = r.Next(1, 10);
hf1.Value = n.ToString();
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApp._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript">
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:Button ID="Button2" runat="server" onclick="bt2_Click" Text="Button" />
<asp:HiddenField ID="hf1" runat="server" />
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:Label ID="lbshow" runat="server"></asp:Label>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e) {
int.TryParse(hf1.Value, out n);
}
protected void Button1_Click(object sender, EventArgs e) {
string k;
k = TextBox1.Text;
if (k == n.ToString()) {
lbshow.Text = "恭喜猜中了!";
Label1.Text = "";
}
else {
lbshow.Text = "很遗憾!没有猜中!";
Label1.Text = "这次的随机为" + n;
}
}
int n;
protected void bt2_Click(object sender, EventArgs e) {
Random r = new Random();
n = r.Next(1, 10);
hf1.Value = n.ToString();
}