For example, show the time in the form:
Wrong idea one: I write a dead loop in the form structure function, showing the current time every second
Public FORM6 () { InitializeComponent (); while (true) { = DateTime.Now.ToString ("yyyy mm month DD day hh" mm min ss sec "); System.Threading.Thread.Sleep (+); } }
structure function Write cycle display time
Error reason: Structure function infinite loop, structure function can't read code is unable to open form (Li brainstorming lxc)
Wrong idea two: Place a button, the form starts to click the button to start to show the time
Public FORM6 () { InitializeComponent (); } Private void button1_click (object sender, EventArgs e) { while (True { = DateTime.Now.ToString ("yyyy mm month DD day hh" mm min ss sec " ) ); System.Threading.Thread.Sleep (+); } }
Click the button to display the time
Error reason: The form system is looping through time and cannot perform other actions on the form
=======================================================
How do I display the current time in a form? --Timer Control (Lee brainstorming lxc)
Timer Control-A component that raises events at user-defined intervals, consumes one thread alone, and the system works
Common Properties:
Enabled-whether the control is enabled
Interval-frequency of events, how long it takes to trigger a time (Li brainstorming lxc)
Execution events: Each event to be triggered
Display the time in the form:
// Timer Control display time Private void Timer1_Tick (object sender, EventArgs e) { = DateTime.Now.ToString (" yyyy mm month dd Day hh mm min ss sec ");
Timer Control display time
===============================================================
Using the timer control to make a random lottery system
PublicFORM6 () {InitializeComponent (); } BOOLStart =false; //start or end a lottery Private voidButton1_Click (Objectsender, EventArgs e) { if(start==false) {timer1. Enabled=true; Start=true; Button1. Text="Stop"; } Else{timer1. Enabled=false; Start=false; Button1. Text="Start"; } } //Timer Control Events Private voidTimer1_Tick (Objectsender, EventArgs e) {Random R=NewRandom (); Label1. Text= R.next (1, $). ToString (); }
Random Draw
C#-winform-timer controls