Home > Net >  A timer control and control process of two events
A timer control and control process of two events

Time:09-17

using System;
using System.Collections.Generic;
Using System.Com ponentModel;
using System.Data;
Using System. Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
Using System. The Timers;

The namespace computer use time
{
Public partial class time: the Form
{
Int shijian=0;
Public time ()
{
InitializeComponent();
}
Public String gettime (int t)
{
Int hh, mm, ss, ms, num.
String h, m, s, MMS;
Num=100 t/;//get the total number of seconds to
Ms=t % 100;
Ss=num % 60;
60 mm=num/60%;
Hh=num/60/60%60;

If (ms & lt; MMS="0" + 10) Ms. ToString ();
The else MMS=Ms. ToString ();

If (ss & lt; 10) s="0" + ss. The ToString ();
The else s=ss. The ToString ();

If (mm & lt; M="0" + 10) mm. The ToString ();
The else m=mm. The ToString ();

If (hh & lt; 10) h="0" + hh. The ToString ();
The else h=hh. ToString ();
String times=h + ":" + m + ":" + s + ":" + MMS;

Return times;
}

Private void time_Load (object sender, EventArgs e)
{

}

Private void button2_Click (object sender, EventArgs e)
{
Label1. Text="00:00:00:00";
}

Private void button1_Click (object sender, EventArgs e)
{

If (timer1. Enabled)
{
Timer1. Stop ();//stop the timer control
For the Text="began to surf the Internet";
Button2. Enabled=true;
}
The else
{
Timer1. Start ();//start the timer control
For the Text="Internet end";
Button2. Enabled=false;
}
}

Private void timer1_Tick (object sender, EventArgs e)
{
Shijian++;
Label1. Text=gettime (shijian);

}

Private void label1_Click (object sender, EventArgs e)
{

}


}
}
To timing, I use a timer control interface is a label control two buttons, the initial value is 00:00:00 label control: 00, then start timing, when click the button a label control value began to change, click on the button button from start the time to stop timing after 1, 2 is reset reset button, I think the effect of the implementation is the time when the inside of the label control pop-up prompt every hour, play the window label control no longer timing at the same time, I try to use two pop-up timer does not stop when the timer control, word frequency jump out of the window and a timer and timer frequency is same, it is better for instance or tutorial, the closer the better, I am a rookie, just contact with c #,

CodePudding user response:

 
Public partial class Form1: Form
{
Public _click ()
{
InitializeComponent();
}

DateTime _dtStart;
Bool _isShow=false;

Private void button1_Click (object sender, EventArgs e)
{
If (timer1. Enabled)
{
Timer1. Stop ();//stop the timer control
For the Text="began to surf the Internet";
Button2. Enabled=true;
}
The else
{
_isShow=true;
_dtStart=DateTime. Now;
Timer1. Start ();//start the timer control
For the Text="Internet end";
Button2. Enabled=false;
}
}

Private void button2_Click (object sender, EventArgs e)
{
Label1. Text="00:00:00:00";
}

Private void timer1_Tick (object sender, EventArgs e)
{
If (_isShow)
{
TimeSpan ts=DateTime. Now - _dtStart;
Label1. Text=the string. Format (" {0}, {1}, {2}, {3} ", padLeft0 (ts) Hours), padLeft0 (ts) Minutes), padLeft0 (ts) Seconds), padLeft0 (ts) Milliseconds/10));
If (ts) TotalHours & gt; 1)
{
_isShow=false;
MessageBox. Show (" the time has come ");
}
}
}

String padLeft0 (int v)
{
Return v.T oString (.) PadLeft (2 '0');
}

}

CodePudding user response:

Bosses, forgive me for not describe the problem clearly, don't call my
For me, I do is a simple timer and button after a click on start time, midway both automatic popup window or click the stop time, label in addition to stop without any changes, click the button again or play window click on the start timing or continue, then the original time only click button2label will reset to zero

CodePudding user response:

Now label control already achieve the result that I want, but the new problem is the label confusion, originally recorded hours now record minutes now, the original record minutes now become a record number of seconds, and time is normal, the late faster and faster, instantly display has arrived for one hour, after click the play button to make the pop-up window disappear label does not change, but in time click on the start button again (for) Windows again immediately after the

CodePudding user response:

 DateTime _dtStart; 
Bool _isShow=false;
TimeSpan _tsNow;

Private void button1_Click (object sender, EventArgs e)
{
If (timer1. Enabled)
{
Timer1. Stop ();//stop the timer control
_tsNow +=DateTime. Now - _dtStart;
For the Text="began to surf the Internet";
Button2. Enabled=true;
}
The else
{
_isShow=true;
_dtStart=DateTime. Now;
Timer1. Start ();//start the timer control
For the Text="Internet end";
Button2. Enabled=false;
}
}

Private void button2_Click (object sender, EventArgs e)
{
Label1. Text="00:00:00:00";
_tsNow=new TimeSpan (0);
}

Private void timer1_Tick (object sender, EventArgs e)
{
If (_isShow)
{
TimeSpan ts=DateTime. Now - _dtStart + _tsNow;
Label1. Text=the string. Format (" {0}, {1}, {2}, {3} ", padLeft0 (ts) Hours), padLeft0 (ts) Minutes), padLeft0 (ts) Seconds), padLeft0 (ts) Milliseconds/10));
If (ts) TotalHours & gt; 1)
{
_isShow=false;
MessageBox. Show (" the time has come ");
}
}
}

String padLeft0 (int v)
{
Return v.T oString (.) PadLeft (2 '0');
}
nullnullnull
  •  Tags:  
  • C #
  • Related