Cronômetro C# Asp.Net
17/11/2010
0
Preciso criar um cronometro em ordem decrescente, por exemplo 00:30 (trinta minutos) e vai decrescendo para 00:29 00:28 a medida que o tempo for passando, e quando chegar em 00:00 executar uma função.
Alguém pode me dar um help?
AT.Marcos Alexandre
Marcos Souza
Posts
18/11/2010
Felix Jr
Já tive um problema parecido com esse. Eu consegui resolver fazendo um Javascript que qdo chegava no zero chamava um evento do c#.
Se servir assim posso postar o código pra vc.
Abraço Felix Jr.
18/11/2010
Vinicius Vieira
namespace Util{ public partial class Cronometro : UserControl { #region Design
/// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null;
/// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); }
#region Component Designer generated code
/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.btnIniciar = new System.Windows.Forms.Button(); this.pnlPrinc = new System.Windows.Forms.Panel(); this.label1 = new System.Windows.Forms.Label(); this.lblDp1 = new System.Windows.Forms.Label(); this.lblSg = new System.Windows.Forms.Label(); this.lblMn = new System.Windows.Forms.Label(); this.lblHr = new System.Windows.Forms.Label(); this.btnPararIniciar = new System.Windows.Forms.Button(); this.btnZerar = new System.Windows.Forms.Button(); this.lblTempoEsgotado = new System.Windows.Forms.Label(); this.panel5 = new System.Windows.Forms.Panel(); this.lblCentesimoSeg = new System.Windows.Forms.Label(); this.panel4 = new System.Windows.Forms.Panel(); this.lblSegundo = new System.Windows.Forms.Label(); this.panel3 = new System.Windows.Forms.Panel(); this.lblMinuto = new System.Windows.Forms.Label(); this.panel2 = new System.Windows.Forms.Panel(); this.lblHora = new System.Windows.Forms.Label(); this.timerCronometro = new System.Windows.Forms.Timer(this.components); this.pnlPrinc.SuspendLayout(); this.panel5.SuspendLayout(); this.panel4.SuspendLayout(); this.panel3.SuspendLayout(); this.panel2.SuspendLayout(); this.SuspendLayout(); // // btnIniciar // this.btnIniciar.Location = new System.Drawing.Point(0, 89); this.btnIniciar.Name = "btnIniciar"; this.btnIniciar.Size = new System.Drawing.Size(103, 28); this.btnIniciar.TabIndex = 0; this.btnIniciar.Text = "Iniciar"; this.btnIniciar.UseVisualStyleBackColor = true; this.btnIniciar.Click += new System.EventHandler(this.btnIniciar_Click); // // pnlPrinc // this.pnlPrinc.BackColor = System.Drawing.SystemColors.ControlDark; this.pnlPrinc.Controls.Add(this.label1); this.pnlPrinc.Controls.Add(this.lblDp1); this.pnlPrinc.Controls.Add(this.lblSg); this.pnlPrinc.Controls.Add(this.lblMn); this.pnlPrinc.Controls.Add(this.lblHr); this.pnlPrinc.Controls.Add(this.btnPararIniciar); this.pnlPrinc.Controls.Add(this.btnZerar); this.pnlPrinc.Controls.Add(this.lblTempoEsgotado); this.pnlPrinc.Controls.Add(this.panel5); this.pnlPrinc.Controls.Add(this.panel4); this.pnlPrinc.Controls.Add(this.panel3); this.pnlPrinc.Controls.Add(this.panel2); this.pnlPrinc.Controls.Add(this.btnIniciar); this.pnlPrinc.Location = new System.Drawing.Point(0, 4); this.pnlPrinc.Name = "pnlPrinc"; this.pnlPrinc.Size = new System.Drawing.Size(309, 136); this.pnlPrinc.TabIndex = 1; // // label1 // this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 30F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.label1.ForeColor = System.Drawing.Color.Red; this.label1.Location = new System.Drawing.Point(177, 36); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(21, 47); this.label1.TabIndex = 12; this.label1.Text = ":"; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // lblDp1 // this.lblDp1.Font = new System.Drawing.Font("Microsoft Sans Serif", 30F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblDp1.ForeColor = System.Drawing.Color.Red; this.lblDp1.Location = new System.Drawing.Point(83, 36); this.lblDp1.Name = "lblDp1"; this.lblDp1.Size = new System.Drawing.Size(21, 47); this.lblDp1.TabIndex = 11; this.lblDp1.Text = ":"; this.lblDp1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // lblSg // this.lblSg.AutoSize = true; this.lblSg.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblSg.ForeColor = System.Drawing.Color.Red; this.lblSg.Location = new System.Drawing.Point(198, 23); this.lblSg.Name = "lblSg"; this.lblSg.Size = new System.Drawing.Size(22, 13); this.lblSg.TabIndex = 10; this.lblSg.Text = "Sg"; // // lblMn // this.lblMn.AutoSize = true; this.lblMn.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblMn.ForeColor = System.Drawing.Color.Red; this.lblMn.Location = new System.Drawing.Point(104, 23); this.lblMn.Name = "lblMn"; this.lblMn.Size = new System.Drawing.Size(24, 13); this.lblMn.TabIndex = 9; this.lblMn.Text = "Mn"; // // lblHr // this.lblHr.AutoSize = true; this.lblHr.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblHr.ForeColor = System.Drawing.Color.Red; this.lblHr.Location = new System.Drawing.Point(3, 23); this.lblHr.Name = "lblHr"; this.lblHr.Size = new System.Drawing.Size(20, 13); this.lblHr.TabIndex = 8; this.lblHr.Text = "Hr"; // // btnPararIniciar // this.btnPararIniciar.Enabled = false; this.btnPararIniciar.Location = new System.Drawing.Point(172, 89); this.btnPararIniciar.Name = "btnPararIniciar"; this.btnPararIniciar.Size = new System.Drawing.Size(67, 28); this.btnPararIniciar.TabIndex = 7; this.btnPararIniciar.Text = "Pausar"; this.btnPararIniciar.UseVisualStyleBackColor = true; this.btnPararIniciar.Click += new System.EventHandler(this.btnPararIniciar_Click); // // btnZerar // this.btnZerar.Location = new System.Drawing.Point(239, 89); this.btnZerar.Name = "btnZerar"; this.btnZerar.Size = new System.Drawing.Size(67, 28); this.btnZerar.TabIndex = 6; this.btnZerar.Text = "Reiniciar"; this.btnZerar.UseVisualStyleBackColor = true; this.btnZerar.Click += new System.EventHandler(this.btnZerar_Click); // // lblTempoEsgotado // this.lblTempoEsgotado.AutoSize = true; this.lblTempoEsgotado.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblTempoEsgotado.ForeColor = System.Drawing.Color.White; this.lblTempoEsgotado.Location = new System.Drawing.Point(88, 120); this.lblTempoEsgotado.Name = "lblTempoEsgotado"; this.lblTempoEsgotado.Size = new System.Drawing.Size(102, 13); this.lblTempoEsgotado.TabIndex = 5; this.lblTempoEsgotado.Text = "Tempo Esgotado"; this.lblTempoEsgotado.Visible = false; // // panel5 // this.panel5.BackColor = System.Drawing.SystemColors.Control; this.panel5.Controls.Add(this.lblCentesimoSeg); this.panel5.Location = new System.Drawing.Point(271, 63); this.panel5.Name = "panel5"; this.panel5.Size = new System.Drawing.Size(35, 20); this.panel5.TabIndex = 4; // // lblCentesimoSeg // this.lblCentesimoSeg.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblCentesimoSeg.ForeColor = System.Drawing.Color.Red; this.lblCentesimoSeg.Location = new System.Drawing.Point(3, 0); this.lblCentesimoSeg.Name = "lblCentesimoSeg"; this.lblCentesimoSeg.Size = new System.Drawing.Size(29, 20); this.lblCentesimoSeg.TabIndex = 0; this.lblCentesimoSeg.Text = "0"; this.lblCentesimoSeg.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // panel4 // this.panel4.BackColor = System.Drawing.SystemColors.Control; this.panel4.Controls.Add(this.lblSegundo); this.panel4.Location = new System.Drawing.Point(195, 36); this.panel4.Name = "panel4"; this.panel4.Size = new System.Drawing.Size(73, 47); this.panel4.TabIndex = 3; // // lblSegundo // this.lblSegundo.Font = new System.Drawing.Font("Microsoft Sans Serif", 30F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblSegundo.ForeColor = System.Drawing.Color.Red; this.lblSegundo.Location = new System.Drawing.Point(3, 0); this.lblSegundo.Name = "lblSegundo"; this.lblSegundo.Size = new System.Drawing.Size(67, 47); this.lblSegundo.TabIndex = 0; this.lblSegundo.Text = "0"; this.lblSegundo.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // panel3 // this.panel3.BackColor = System.Drawing.SystemColors.Control; this.panel3.Controls.Add(this.lblMinuto); this.panel3.Location = new System.Drawing.Point(107, 36); this.panel3.Name = "panel3"; this.panel3.Size = new System.Drawing.Size(71, 47); this.panel3.TabIndex = 2; // // lblMinuto // this.lblMinuto.Font = new System.Drawing.Font("Microsoft Sans Serif", 30F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblMinuto.ForeColor = System.Drawing.Color.Red; this.lblMinuto.Location = new System.Drawing.Point(3, 0); this.lblMinuto.Name = "lblMinuto"; this.lblMinuto.Size = new System.Drawing.Size(67, 47); this.lblMinuto.TabIndex = 0; this.lblMinuto.Text = "0"; this.lblMinuto.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // panel2 // this.panel2.BackColor = System.Drawing.SystemColors.Control; this.panel2.Controls.Add(this.lblHora); this.panel2.Location = new System.Drawing.Point(3, 36); this.panel2.Name = "panel2"; this.panel2.Size = new System.Drawing.Size(76, 47); this.panel2.TabIndex = 1; // // lblHora // this.lblHora.Font = new System.Drawing.Font("Microsoft Sans Serif", 30F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblHora.ForeColor = System.Drawing.Color.Red; this.lblHora.Location = new System.Drawing.Point(3, 0); this.lblHora.Name = "lblHora"; this.lblHora.Size = new System.Drawing.Size(67, 47); this.lblHora.TabIndex = 0; this.lblHora.Text = "0"; this.lblHora.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // timerCronometro // this.timerCronometro.Interval = 10; this.timerCronometro.Tick += new System.EventHandler(this.timerCronometro_Tick); // // Cronometro // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.pnlPrinc); this.Name = "Cronometro"; this.Size = new System.Drawing.Size(315, 143); this.pnlPrinc.ResumeLayout(false); this.pnlPrinc.PerformLayout(); this.panel5.ResumeLayout(false); this.panel4.ResumeLayout(false); this.panel3.ResumeLayout(false); this.panel2.ResumeLayout(false); this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button btnIniciar; private System.Windows.Forms.Panel pnlPrinc; private System.Windows.Forms.Panel panel3; private System.Windows.Forms.Label lblMinuto; private System.Windows.Forms.Panel panel2; private System.Windows.Forms.Label lblHora; private System.Windows.Forms.Panel panel4; private System.Windows.Forms.Label lblSegundo; private System.Windows.Forms.Panel panel5; private System.Windows.Forms.Label lblCentesimoSeg; private System.Windows.Forms.Timer timerCronometro; private System.Windows.Forms.Label lblTempoEsgotado; private System.Windows.Forms.Button btnZerar; private System.Windows.Forms.Button btnPararIniciar; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label lblDp1; private System.Windows.Forms.Label lblSg; private System.Windows.Forms.Label lblMn; private System.Windows.Forms.Label lblHr;
#endregion Design public Cronometro() { InitializeComponent(); ChamaEvento = new DelVoid(ConChamaEvento); }
private int _TempoSegundos; private int _TempoDecorrido;
public int TempoSegundos { get { return _TempoSegundos; } set { _TempoSegundos = value; SeparaTempo(_TempoSegundos * 100); } } //Aqui define o evento! public delegate void TempoEsgotadoHandler(object sender, EventArgs e); public event TempoEsgotadoHandler TempoEsgotado;
public delegate void DelVoid(); public DelVoid ChamaEvento;
private void ConChamaEvento() {
TempoEsgotado(this, new EventArgs()); } private void btnIniciar_Click(object sender, EventArgs e) { btnZerar_Click(sender, e); btnPararIniciar.Enabled = true; timerCronometro.Enabled = true; btnPararIniciar.Text = "Pausar"; }
private void timerCronometro_Tick(object sender, EventArgs e) { SeparaTempo((_TempoSegundos * 100) - _TempoDecorrido); if (_TempoDecorrido == (_TempoSegundos * 100)) { lblTempoEsgotado.Visible = true; timerCronometro.Enabled = false; //Aqui chama o método que invoca o evento, mas não precisa de ter um método eu só fiz assim pq tava em outra thread(por causa do timer) if (TempoEsgotado != null) this.Invoke(ChamaEvento);//Se for na mesma thread é só colocar : TempoEsgotado(this, new EventArgs());
btnPararIniciar.Enabled = false; btnPararIniciar.Text = "Pausar"; } _TempoDecorrido++;
}
private void btnZerar_Click(object sender, EventArgs e) { SeparaTempo(_TempoSegundos * 100); lblTempoEsgotado.Visible = false; _TempoDecorrido = 0; btnPararIniciar.Enabled = false; btnPararIniciar.Text = "Pausar"; timerCronometro.Enabled = false; }
private void SeparaTempo(int centessimosSeg) { int Hora, Minuto, Segundo, centesimo; centesimo = centessimosSeg % 100; Segundo = ((centessimosSeg - centesimo) / 100) % 60; Minuto = ((((centessimosSeg - centesimo) / 100) - Segundo) / 60) % 60; Hora = (((((centessimosSeg - centesimo) / 100) - Segundo) / 60) - Minuto) / 60; lblHora.Text = Hora.ToString(); lblMinuto.Text = Minuto.ToString(); lblSegundo.Text = Segundo.ToString(); lblCentesimoSeg.Text = centesimo.ToString(); }
private void btnPararIniciar_Click(object sender, EventArgs e) { timerCronometro.Enabled = timerCronometro.Enabled != true; if (timerCronometro.Enabled == true) btnPararIniciar.Text = "Pausar"; else btnPararIniciar.Text = "Retomar"; }
}}
21/03/2011
Alejandro González
27/03/2011
Rodrigo Odasaki
Você pode fazer isso utilizando javascript.
A data de vencimento do produto fica gravada obviamente na base de dados.
Simplesmente ele começa a contar o cronometro a partir da data atual do (Servidor) para a data de vencimento que está na base de dados.
Clique aqui para fazer login e interagir na Comunidade :)