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();
}
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();
}
}
Here's the pseudo code:
private void ForgetSave()
{
if (the SaveRegularly method hasn't been used within 3 mins)
MessageBox.Show("Would you like to save any changes before closing?")
......... the code continues.
}
else
{
this.close();
}
Does anybody know how to write the first line of the if statement?
Simply remember when the last save time was:
private const TimeSpan saveTimeBeforeWarning = new TimeSpan(0,1,0); //1 minute
private static DateTime _lastSave = DateTime.Now;
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if ((DateTime.Now - _lastSave) > saveTimeBeforeWarning)
{
if(MessageBox.Show("Would you like to save any changes before closing?") == DialogResult.Yes);
{
Save();
}
}
}
private void Save()
{
//save data
_lastSave = DateTime.Now
}
As Ahmed suggested you can use a timer and a flag to know when you have to display the message, I left you a piece of code to get you started
private const int SAVE_TIME_INTERVAL = 3 * 60 * 1000;
private bool iWasSavedInTheLastInterval = true;
private System.Windows.Forms.Timer timer;
public Form1()
{
InitializeComponent();
//Initialize the timer to your desired waiting interval
timer = new System.Windows.Forms.Timer();
timer.Interval = SAVE_TIME_INTERVAL;
timer.Tick += Timer_Tick;
timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
//If the timer counts that amount of time we haven't saved in that period of time
iWasSavedInTheLastInterval = false;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (iWasSavedInTheLastInterval == false)
{
MessageBox.Show("Would you like to save any changes before closing?");
}
}
private void btnSave_Click(object sender, EventArgs e)
{
//If a manual save comes in then we restart the timer and set the flag to true
iWasSavedInTheLastInterval = true;
timer.Stop();
timer.Start();
}
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);
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");
}
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";
}
}