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");
}
Related
I am quite new to using the chart coding.But i manage to display chart on a click of the button. And 2nd click will change the "x-axis" as i set it as time.
Is there a way to auto update every 5 for the "X-axis" ?? As i know timer is needed to set the interval timing.
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Start();
this.Time.Text = System.DateTime.Now.ToString("hh:mm:ss tt");
}
private void GetData_Click(object sender, EventArgs e)
{
sqlite_con1.Open();
try
{
sqlite_cmd = sqlite_con1.CreateCommand();
sqlite_cmd.CommandText = "SELECT * FROM Temperature where id=12";
sqlite_reader = sqlite_cmd.ExecuteReader();
while (sqlite_reader.Read())
{
this.chart1.Series["SAT"].Points.AddXY(Time.Text, sqlite_reader["Temp"]);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
sqlite_con1.Close();
}
Any help is much appreciated. Thanks.
you have to set timer time and sets its property and event handler into timer at specific time
Timer t = new Timer();
t.Interval = 5000; // for five second interval
timer1.Enabled = true;
timer1.Tick += new System.EventHandler(timer1_Tick);
and then you have to call you chart binding method in
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Start();
this.Time.Text = System.DateTime.Now.ToString("hh:mm:ss tt");
//here --- which you are calling on GetData_Click event by making
//separate method and call it here also
}
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);
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;
}
In my project I would like to show message or call methods after 5 minutes for example, If the users didn't click on specific button, I wrote this code
Boolean flage = false;
private void button1_Click(object sender, EventArgs e)
{
Timer Clock;
Clock = new System.Windows.Forms.Timer();
Clock.Interval = 1000;
Clock.Start();
Clock.Tick += new EventHandler(Timer_Tick);
}
public void Timer_Tick(object sender, EventArgs eArgs)
{
if (flage == false)
{
MessageBox.Show("after period of time ");
}
}
private void button2_Click(object sender, EventArgs e)
{
flage = true;
}
Its keeping show the messageBox can any body help me.
Your Timer Clock variable is on the stack and ceases to exist when the function exits.
Try making it a member of the class.
I have a label. I need to change the text property every 3 seconds. Please let me know how to do this. I tried using timer, but my application is going into infinite loop. I do not want this to happen/ Any help will be appreciated!
timer1.Interval = 5000;
timer1.Enabled = true;
timer1.Tick += new System.EventHandler (OnTimerEvent);
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
refreshStatusBar();
}
In your class constructor, you need to initialize the initial text for the Label and the .NET Framework's Timer component.
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = (1000) * (3); // Timer will tick every 3 seconds
timer.Enabled = true;
timer.Start();
label.Text = DateTime.Now.ToString(); // initial label text.
Then in the timer's tick handler, update the Label's text property.
private void timer_Tick(object sender, ElapsedEventArgs e)
{
label.Text = DateTime.Now.ToString(); // update text ...
}
You should use thread, and when you want to stop call yourthread.Abort();
Update: SynchronizationContext method:
System.Threading.SynchronizationContext sync;
private void Form1_Load(object sender, System.EventArgs e)
{
sync = SynchronizationContext.Current;
System.Windows.Forms.Timer tm = new System.Windows.Forms.Timer { Interval = 1000 };
tm.Tick += tm_Tick;
tm.Start();
}
//Handles tm.Tick
private void tm_Tick(object sender, System.EventArgs e)
{
sync.Post(dopost, DateAndTime.Now.ToString());
}
public void dopost(string txt)
{
Label1.Text = txt;
}