I am working on the reminder app I need to start the timer so that after the timer gets over it reminds me the events set by me.
In the image I have encircle the timer.
DispatcherTimer timer = new DispatcherTimer();
timer.Interval=TimeSpan.
private int Time;
DispatcherTimer timer;
private void TextBlock_Loaded(object sender, RoutedEventArgs e)
{
}
void timer_Tick(object sender, EventArgs e)
{
if (Time > 0)
{
Time--;
timer.Interval = TimeSpan.FromSeconds(1);
Debug.WriteLine(" " + Time + " \n");
}
}
private void TextBox_Loaded(object sender, RoutedEventArgs e)
{
Time = ((sender as FrameworkElement).DataContext as PersonalModel).RemainingHours;
timer = new DispatcherTimer();
timer.Start();
timer.Tick -= timer_Tick;
timer.Tick += timer_Tick;
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
((sender as FrameworkElement)
}
follow this tutorial for how you can set reminders and alert alarms in windows phone 8
and here is the code for setting the reminder in windows phone 8.
Reminder reminder = new Reminder(name);
reminder.Title = titleTextBox.Text;
reminder.Content = contentTextBox.Text;
reminder.BeginTime = beginTime; // it is the time when remider will start reminding(e.g remind me after 8 days and 2 AM hours you will set it DateTime.Now.Date.AddDays(8).AddHours(2)
reminder.ExpirationTime = expirationTime;
reminder.RecurrenceType = recurrence;
reminder.NavigationUri = navigationUri;
// Register the reminder with the system.
ScheduledActionService.Add(reminder);
Related
I'm coding a Family Feud game in C# and after each answer is submitted the 30-second timer is supposed to reset. The problem is I'm accumulating 1-second Intervals so that the counter is counting down faster and faster. I can't figure out how to prevent the Intervals from accumulating. I want it to stay a fixed 1 seconds no matter how many times the button is pressed.
Code:
public System.Windows.Forms.Timer _timer = new System.Windows.Forms.Timer();
public int time = 30;
private void Check_Click(object sender, RoutedEventArgs e)
{
time = 30;
_timer.Tick += timer_Tick;
_timer.Start();
uxLabel1.Content = time.ToString();
//additional code
}
private void timer_Tick(object sender, EventArgs e)
{
_timer.Interval = 1000;
time--;
if (time == 0)
{
_timer.Stop();
strike();
}
uxLabel1.Content = time.ToString();
}
Don't put _timer.Tick += timer_Tick; in the button click code, put it once in the constructor of your form and have that be the only event registration.
public partial class YourForm : Form
{
public YourForm()
{
InitializeComponets();
_timer = new System.Windows.Forms.Timer();
_timer.Tick += timer_Tick;
_timer.Interval = 1000;
}
private System.Windows.Forms.Timer _timer;
private int time = 30;
private void Check_Click(object sender, RoutedEventArgs e)
{
time = 30;
_timer.Start();
uxLabel1.Content = time.ToString();
//additional code
}
private void timer_Tick(object sender, EventArgs e)
{
time--;
if (time == 0)
{
_timer.Stop();
strike();
}
uxLabel1.Content = time.ToString();
}
}
How can I change the text of button with timeout? I tried out with the following code but it is not working.
private void button1_Click(object sender, EventArgs e)
{
Stopwatch sw = new Stopwatch();
sw.Start();
if (button1.Text == "Start")
{
//do something
button1.Text = "stop"
if (sw.ElapsedMilliseconds > 5000)
{
button1.Text = "Start";
}
}
How can I correct my code?
You need to use Timer instead:
Timer t = new Timer(5000); // Set up the timer to trigger on 5 seconds
t.SynchronizingObject = this; // Set the timer event to run on the same thread as the current class, i.e. the UI
t.AutoReset = false; // Only execute the event once
t.Elapsed += new ElapsedEventHandler(t_Elapsed); // Add an event handler to the timer
t.Enabled = true; // Starts the timer
// Once 5 seconds has elapsed, your method will be called
void t_Elapsed(object sender, ElapsedEventArgs e)
{
// The Timer class automatically runs this on the UI thread
button1.Text = "Start";
}
Stopwatch is only for measuring how much time has passed since you called Start().
If you're using C# 5
private async void button1_Click(object sender, EventArgs e)
{
button1.Text = "Stop";
await Task.Delay(5000);
button1.Text = "Start";
}
You could use a timer. In this example the text of the button changes to "Stop" after 5 seconds.
private Timer timer = new Timer();
private void button1_Click(object sender, EventArgs e)
{
timer.Interval = 5000; // interval length
timer.Tick += TimerOnTick;
timer.Enabled = true; // activate timer
button1.Text = "Start";
}
private void TimerOnTick(object sender, EventArgs eventArgs)
{
timer.Enabled = false; // deactivate timer
button1.Text = "Stop";
}
I think you can reach your goal by using Timer
Example of using Timer
public partial class FormWithTimer : Form
{
Timer timer = new Timer();
public FormWithTimer()
{
InitializeComponent();
// Everytime timer ticks, timer_Tick will be called
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = (1000) * (1); // Timer will tick every second
timer.Enabled = true; // Enable the timer
}
// .......
showForm() // declaration
{
timer.start();
// .......
timer.stop();
}
void timer_Tick(object sender, EventArgs e)
{
//hide form...through visibility
}
}
Use this instead of Stopwatch:
private void button1_Click(object sender, EventArgs e)
{
button1.Text = "stop"
aTimer = new System.Timers.Timer(5000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Enabled = true;
}
// Specify what you want to happen when the Elapsed event is raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
button1.Text = "Start";
var atim = source as Timer;
if (atim != null)
atim.Elapsed -= OnTimedEvent;
}
C# code
TextTime.Text = DateTime.Now.ToString();
Want to refresh this text box every second
Or show a Digital Clock any Idea
You may use a DispatcherTimer:
var timer = new DispatcherTimer
{
Interval = TimeSpan.FromSeconds(1.0)
};
timer.Tick += (o, e) =>
{
TextTime.Text = DateTime.Now.ToString();
};
timer.Start();
The easy way is to add a timer to your app and do it as shown:
Form Load:
private void Form1_Load(object sender, EventArgs e) {
txtdate.Text = DateTime.Now.ToString(("dddd" + ("," + "MM-dd-yyyy")));
Timer1.Interval = 1000;
Timer1.Enabled = true;
}
Timer Tick:
private void Timer1_Tick(object sender, EventArgs e) {
txtTime.Text = DateTime.Now.ToString("HH:mm:ss");
}
the code below works in Windows Phone 7
private void ShowTime()
{
txtTime.Text = get24hour();
//display the Date and week.
DateTime nowtime = DateTime.Now;
txtWeek.Text = nowtime.DayOfWeek.ToString();
txtDate.Text = nowtime.Date.ToString("MM/dd");
//create timer to fresh to time
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMinutes(1);
timer.Tick += timer_Ticker;
timer.Start();
}
private void timer_Ticker(object sender, EventArgs e)
{
txtTime.Text = get24hour();
}
private string get24hour()
{
return DateTime.Now.ToString("HH:mm");
}
but error in WinRT (Metro)
error part:
timer.Tick += timer_Ticker;
error message:
No overload for 'timer_Ticker' matches delegate 'System.EventHandler<object>'
what I do
I try to change the code to
private void timer_Ticker()
{
txtTime.Text = get24hour();
}
result
but it is not work again, why and how to solve it? :(
timer.Tick += new EventHandler<object>(timer_Tick);
private void timer_Tick(object sender, object e)
{
}
Refer to this link
I read the msdn and change the delegate method to below and it works:
private void timer_Ticker(object sender, object e)
{
txtTime.Text = get24hour();
}
Hi I am working with Windows.Forms.Timer with Web Application . I create Timer.Tick event handler to handle Timer_Tick but I am not successfull. I don't get any error but I can not get result even. Here is my code
System.Windows.Forms.Timer StopWatchTimer = new System.Windows.Forms.Timer();
Stopwatch sw = new Stopwatch();
public void StopwatchStartBtn_Click(object sender, ImageClickEventArgs e)
{
StopWatchTimer.Enabled = true;
StopWatchTimer.Interval = 1;
StopWatchTimer.Start();
this.StopWatchTimer.Tick += new EventHandler(StopWatchTimer1_Tick);
sw.Start();
}
protected void StopWatchStopBtn_Click(object sender, ImageClickEventArgs e)
{
StopWatchTimer.Stop();
sw.Reset();
StopWatchLbl.Text = "00:00:00:000";
}
public void StopWatchTimer1_Tick(object sender,EventArgs e)
{
TimeSpan elapsed = sw.Elapsed;
StopWatchLbl.Text = string.Format("{0:00}:{1:00}:{2:00}:{3:00}",
Math.Floor(elapsed.TotalHours),
elapsed.Minutes,
elapsed.Seconds,
elapsed.Milliseconds);
}
From the MSDN documentation for Windows Forms Timer (emphasis mine):
Implements a timer that raises an event at user-defined intervals. This timer is optimized for use in Windows Forms applications and must be used in a window.
This timer will not work in a web application. You'll need to use another class, like System.Timers.Timer. This has it's own pitfalls, however.
Did you try defining the Tick event prior to starting the timer?
this.StopWatchTimer.Tick += new EventHandler(StopWatchTimer1_Tick);
StopWatchTimer.Start();
public partial class TestFrom : Form
{
private Thread threadP;
private System.Windows.Forms.Timer Timer = new System.Windows.Forms.Timer();
private string str;
public TestFrom()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Timer.Interval =100;
Timer.Tick += new EventHandler(TimeBussiness);
Timer.Enabled = true;
Timer.Start();
Timer.Tag = "Start";
}
void TimeBussiness(object sender, EventArgs e)
{
if (threadP.ThreadState == ThreadState.Running)
{
Timer.Stop();
Timer.Tag = "Stop";
}
else
{
//do my bussiness1;
}
}
private void button3_Click(object sender, EventArgs e)
{
ThreadStart threadStart = new ThreadStart(Salver);
threadP= new Thread(threadStart);
threadP.Start();
}
private void Salver()
{
while (Timer.Tag == "Stop")
{
}
//do my bussiness2;
Timer.Start();
Timer.Tag = "Start";
}
}