This is the 19th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021
preface
A few days ago, I found a Timer control and ProgressBar control, so I decided to make a countdown Timer. C# forms app is a fun language, you have to have fun in it, because in this language, you can feel what you do immediately, there is a wysiwyg. Blogger this article is just a simple explanation, inspiration, the blogger just made a simple timer, you can according to your own ideas to make a timer or a similar electronic watch like the clock, beginners finished you will feel a sense of achievement feel so language is also broad.
Once a day, happy day!!
Initial display effect: Make you look back hahaha
1. Create form application files and design the interface
Create a form class. See the previous article 🙌🙌🙌
1.1 Create and drag ComboBOX control Settings properties
Here we set the static, no-input ComboBOX, which has three properties for the ComboBOX display style
Simple is text editable and the drop-down list is always visible; DropDown is the default, editable text that requires the user to click an arrow. The DropDownList is a non-editable text that requires the user to click the arrow. Combobox1.text = "value "; The result of the assignment is null,Copy the code
1.2 Setting the ProgressBar Control
When we use the progress bar control, we need to set the maximum Value of the progress bar, the amount of step, and the Value properties. The blogger has set zero in the call to facilitate repeated calls.
Note the Maximum property of the ProgressBar control: it sets or returns the Maximum value that the ProgressBar can display. The default value is 100. Minimum attribute: Used to set or return the Minimum value that the progress bar can display. The default value is 0. The MarqueeAnimationSpeed property: this property, usually in milliseconds, shows how fast the load is taking place. The Step property sets or returns a Value that determines how much the Value property increases each time the PerformStep method is called. For example, if you want to copy a set of files, you can set the value of the Step property to 1 and the value of the Maximum property to the total number of files to copy. When copying each file, you can call the PerformStep method and add the Value property of the progress bar to the Value of the Step property: This property displays the progress of the control. If it is 0, the progress is 0; if it is 100, the progress is 100%.Copy the code
1.3 Setting the Time Timer Control
Timer control needs to set the interrupt value, the blogger set 1 second, that is, 1 second stop to achieve the countdown effect, first to open the Timer control to use oh, use start to open oh
Reset () : Stops a running timer, resets currentCount=0, and after calling start() again, the timer instance will run for the specified number of repetitions start() : starts the timer if it has not already run stop () : stops the timer. If start() is called after stop(), the Timer instance will continue to run for the remaining number of repetitions (set by repeatCount property) Interval: Property specifies the Interval (in milliseconds) between Timer events on the form // stop actionCopy the code
1.4 Code analysis for the remaining time
The blogger changed the effect of the remaining time, changing the remaining time with each selection. The default is 0 seconds.
1.5 Analysis of Timer control code
1.6 Code processing for countdown button code trigger
1.6 Overall code and operation effect
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; Namespace countdown {public partial class Form1: Form {public Form1() {InitializeComponent(); } int count = 0; Int time = 0; int time = 0; Private void Form1_Load(Object sender, EventArgs e) private void Form1_Load(object sender, EventArgs e) for(i=1; i<100; I++)// assign 1-99 seconds to comboBox {combobox1.items.Add(i.tostring () + "seconds "); }} private void timer1_Tick(object sender, EventArgs e) {count++; Label3. Text = (time-count).toString () + "second "; Progressbar1. Value = count; Timer1.stop (); if(count==time)// If (count==time) / / end stop time control System. Media. SystemSounds. Asterisk. The Play (); MessageBox.Show(" Time is up! ") ); }} private void button1_Click(Object sender, EventArgs e) {count = 0; progressBar1.Value = 0; String STR = combobox1.text; string STR = combobox1.text; time = Convert.ToInt32(str.Substring(0,(str.Length-1))); // messagebox.show (time.tostring ()); progressBar1.Maximum = time; Timer1.start (); // The timer function is looped when it starts, } private void comboBox1_SelectedIndexChanged(Object Sender, EventArgs e) {label3.Text = combobox1.text; // Change the default value of the remaining time}}}Copy the code
Meikai second effect display:
conclusion
Blogger, this article mainly USES the two controls, a progress bar is a progressBar control is another time the Timer control, to achieve the effect is also fun, for beginners learning form small program application is a good introduction, the main feeling nothing difficult, if you use other languages may be difficult, ha, ha, ha, on the way, We use the Timer control a lot, in the form application, similar to loop, the advantage is that you can stop the effect, for example, you need to do any receive processing, you can use this control every number of seconds, because it can interrupt. Ok, creation is not easy to like.