C#-winform-timer controls

Source: Internet
Author: User

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

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.