WinForm, guess what!

Source: Internet
Author: User

Description

1> form Application.

2> a form (FORM1), a button (btnstart), a text (labtime)

3>:



The 4> code is as follows:

Using System;

Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using System.Data.SqlClient;
Using System.Collections;

Namespace Caicaikan {
Public partial class Form1:form {
Public Form1 () {
InitializeComponent ();
}

System.Threading.Thread gthread;//Defining threads
Random grandom = new Random ();//defines the number object
int gnum;//Define variables to hold the number of storage machines


private void btnStart_Click (object sender, EventArgs e) {
Removecontrol ();//emptying all useless objects
btnstart.enabled = false;//Set Start button is not available
Generate 100 buttons, 10 horizontal 10 Vertical
int px = 10;//The position of the first button
int py = 60;
for (int i = 0; i <; i++) {
Button btn = New button ();//Assign values to buttons, define width and height
Btn. Name = (i+1). ToString ();
Btn. Text = (i+1). ToString ();
Btn. Width = 35;
Btn. Height = 35;
Btn. Location = new Point (px, py);
Btn. Click + = new EventHandler (btn_click);//define button Events
px + = 36;
if ((i + 1)% 10 = = 0) {
px = 10;
py + = 36;
}
Controls.Add (BTN);//Place button button into form control collection
}


Create a new thread
Gthread = new System.Threading.Thread (delegate ()
{
int count = 0;//initialization counter
while (true)//start infinite loop
{
Counter summation
Count = ++count > 100000000? 0:count;//Setting the maximum value
This. Invoke (//give the code to the main thread for execution
(methodinvoker) delegate//using anonymous methods
{
Display count in form
Labtime.text = "Spents:" + count. ToString () + "second";
});
Thread Sleep 1 seconds
System.Threading.Thread.Sleep (1000);
}
});
Gthread.isbackground = true;//set thread as background thread
Gthread.start ();//Start thread execution
Gnum = Grandom.next (1, 100);//Generate random numbers
}

Used to empty dynamically generated buttons in a form
void Removecontrol ()
{
for (int i = 0; i < i++)//Start traversal of 100 buttons
{
if (Controls.containskey (i + 1). ToString ()))//Whether this button is available in the form
{
for (int j = 0; J < Controls.Count; J + +)//Traverse form Control collection
{
if (Controls[j]. Name = = (i + 1). ToString ())//whether to find the button
{
Controls.removeat (j);//delete the specified button
Break
}
}
}
}
}

Auto-generated button events
void Btn_click (object sender, EventArgs e)
{
Control P_control = sender as control;//converts sender (holding a pointer to an entity object) to the control type Object
if (int. Parse (P_control. Name) > Gnum)
{
P_control. BackColor = color.red;//Set button background is red
P_control. Enabled = false;//Set button deactivated
P_control. Text = "large";//Change button text
}
if (int. Parse (P_control. Name) < Gnum)
{
P_control. BackColor = color.red;//Set button background is red
P_control. Enabled = false;//Set button deactivated
P_control. Text = "small";//Change button text
}
if (int. Parse (P_control. Name) = = Gnum)
{
Gthread.abort ();//Terminating Count thread
Show Game Information
MessageBox.Show (String. Format ("Congratulations, you guessed it!" Total guess {0} times {1} ", GetCount (), Labtime.text)," Congratulations! ");
btnstart.enabled = true;//Enable Start button

}
}

Get the number of clicked buttons
String GetCount ()
{
int temp = 0;//initialization counter
foreach (Control C in controls)//Traversal control collection
{
The number of control that is used to find the Enable property to False in the form
if (!c.enabled) {
temp++;//Counter Summation
}
}
Return temp. ToString ();//Returns counter information
}


Close the form
private void Form1_formclosing (object sender, FormClosingEventArgs e)
{
Environment.exit (0);//forcibly closes the form, the application forces the exit.
}


}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.