Reputation: 27
I made a Webform that displays time and date, and I wanted to do the same for a Stopwatch, but for some reason the Stopwatch doesn't work.
The time is supposed to start ticking when I click "start", it's supposed to stop when I click "stop", and it is supposed to clear when I click "reset". Instead, when I click start, nothing happens.
Here is my code (C#):
{
public partial class WebForm1 : System.Web.UI.Page
{
private short _secs, _ms;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Timerz.Text = DateTime.Now.ToString();
}
protected void Timer2_Tick(object sender, EventArgs e)
{
IncreaseMS();
ShowTime();
}
private void IncreaseMS()
{
if (_ms == 99)
{
_ms = 0;
IncreaseSecs();
}
else
{
_ms++;
}
}
private void IncreaseSecs()
{
if (_secs == 59)
{
_secs = 0;
}
else
{
_secs++;
}
}
protected void start_Click(object sender, EventArgs e)
{
start.Enabled = false;
Timer2.Enabled = true;
}
protected void reset_Click(object sender, EventArgs e)
{
_secs = 0;
_ms = 0;
ShowTime();
}
private void ShowTime()
{
secsText.Text = _secs.ToString("00");
msText.Text = _ms.ToString("00");
}
protected void stop_Click(object sender, EventArgs e)
{
start.Enabled = true;
Timer2.Enabled = false;
}
}
}
Webform, ASP.NET Code:
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
Current time:
<asp:Label ID="Timerz" runat="server"></asp:Label>
</div>
<asp:Timer ID="Timer1" runat="server" Interval="1000"
OnTick="Timer1_Tick">
</asp:Timer>
<div>
<asp:Label ID="secsText" runat="server" Text="00"></asp:Label><asp:Label
ID="colon" runat="server" Text=":"></asp:Label><asp:Label ID="msText"
runat="server" Text="00"></asp:Label>
<br />
<asp:Button ID="start" runat="server" Text="Start" OnClick="start_Click"
/><asp:Button ID="stop" runat="server" Text="Stop" OnClick="stop_Click" />
<asp:Button ID="reset" runat="server" Text="Reset" OnClick="reset_Click" />
</div>
<asp:Timer ID="Timer2" runat="server" OnTick="Timer2_Tick">
</asp:Timer>
</form>
</body>
Upvotes: 0
Views: 251
Reputation: 743
You haven't set the Interval property on Timer2. The default interval is 60 seconds and you are probably not waiting that long.
Upvotes: 1