I have a code like this in windows phone
private void btnrun_Click(object sender, RoutedEventArgs e)
{
t = 0;
Start_timer();
}
public void Start_timer()
{
timer2 = new DispatcherTimer();
timer2.Tick += timer_Tick2;
timer2.Interval = new TimeSpan(0, 0, 0, 0, 1000);
timer2.Start();
}
void timer_Tick2(object sender, object e)
{
t++;
txttime.Text = t.ToString();
}
At first use, the display right from 1,2,3,4 ... but when you click again, it starts from 2, 4, 6 .... click again, the display 3, 6 ...
Is there a way for it to run your display when clicking the button 1,2,3,4 each failure?
thank!!!
Try something like this:
private void btnrun_Click(object sender, RoutedEventArgs e)
{
t = 0;
Start_timer();
}
public void Start_timer()
{
if (timer2 != null)
{
timer2 -= timer_Tick2; // unassigns the event handler
timer2.Stop(); // stops the timer
}
timer2 = new DispatcherTimer();
timer2.Tick += timer_Tick2;
timer2.Interval = new TimeSpan(0, 0, 0, 0, 1000);
timer2.Start();
}
void timer_Tick2(object sender, object e)
{
t++;
txttime.Text = t.ToString();
}
EDIT: If you just want to start the timer once, it could be reduced to:
private void btnrun_Click(object sender, RoutedEventArgs e)
{
t = 0;
if (timer2 == null)
Start_timer();
}
public void Start_timer()
{
timer2 = new DispatcherTimer();
timer2.Tick += timer_Tick2;
timer2.Interval = new TimeSpan(0, 0, 0, 0, 1000);
timer2.Start();
}
void timer_Tick2(object sender, object e)
{
t++;
txttime.Text = t.ToString();
}
Use Stop() method to stop the timer every click before run it again.
private void btnrun_Click(object sender, RoutedEventArgs e)
{
t = 0;
timer2.Stop();
Start_timer();
}
Each timer registers on the same Tick callback:
timer2.Tick += timer_Tick2;
so it's the reason of the problem.
Related
private void OnTimer(object sender, EventArgs e)
{
int i = 0;
i += 1;
}
I tried to make the timer
private void Form1_Load(object sender, EventArgs e)
{
int i = 0;
FontFamily[] fontlar = FontFamily.Families;
fontlar.GetUpperBound(i);
Timer timer1 = new Timer
{
Interval = 750
};
timer1.Enabled = true;
timer1.Tick += new System.EventHandler(OnTimer);
}
I put the code in form1 but I tried it in textbox as well it didn't work
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
button
I guess you are new to this.
Your Timer does nothing
Your Timer is local, it should be public
You didn't create your Textbox
I think this would be your answer:
Timer timer1;
int i = 0;
private void Form1_Load(object sender, EventArgs e)
{
timer1 = new Timer
{
Interval = 750
};
timer1.Enabled = true;
timer1.Tick += new System.EventHandler(OnTimer);
}
private void OnTimer(object sender, EventArgs e)
{
i += 1;
FontFamily[] fontlar = FontFamily.Families;
//fontlar.GetUpperBound(i);
textBox1.Font = new Font(fontlar[i], 16.0f);
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
Don't forget to create a textbox on your form.
I have some MediaElements in my WPF-application
If i call Question1_5.Play(); then Timer_activated.Play(); does not play on time (this audio plays 5-6 seconds after calling Play() method).
What the problem?
private void NewGame_MouseLeftButtonDown(object sender,
MouseButtonEventArgs e) {
Dispatcher.BeginInvoke((Action)(() => Windows.SelectedItem = Game));
Questions1_5.Play();
sb = FindResource("TimerAppear") as Storyboard;
sb.Begin();
sb.Pause();
timer = new DispatcherTimer();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = new TimeSpan(0, 0, 4);
timer.Start();
}
private void timer_Tick(object sender, EventArgs e) {
Timer_activated.Play();
sb.Resume();
sb = FindResource("Seconds15") as Storyboard;
timer.Stop();
}
I have 5 Forms, 1 main form, and 4 forms I want them to switch between each other Automatically every couple of seconds (take turns, each form x seconds and switch to the next).
I have 2 forms so far switching between each other every 2 seconds.
void mytimer_Tick(object sender, EventArgs e)
{
if (!frm2.Focused)
frm2.Focus();
else
frm3.Focus();
}
private void Form1_Load_1(object sender, EventArgs e)
{
Timer mytimer = new Timer();
mytimer.Tick += mytimer_Tick;
mytimer.Interval = 2000;
mytimer.Start();
}
Thankyou.
Crude format. But you will get the idea.
private void HideAllForms()
{
frm1.Hide();
frm2.Hide();
frm3.Hide();
frm4.Hide();
}
void mytimer_Tick(object sender, EventArgs e)
{
if (frmSrl == 1)
{
frmSrl++;
HideAllForms();
frm1.Show();
}
else if (frmSrl == 2)
{
frmSrl++;
HideAllForms();
frm2.Show();
}
else if (frmSrl == 3)
{
frmSrl++;
HideAllForms();
frm3.Show();
}
else if (frmSrl == 4)
{
frmSrl =1;
HideAllForms();
frm4.Show();
}
else
frmSrl = 1;
}
int frmSrl = 1;
private void Form1_Load_1(object sender, EventArgs e)
{
Timer mytimer = new Timer();
mytimer.Tick += mytimer_Tick;
mytimer.Interval = 2000;
mytimer.Start();
}
I want to run a progress bar on a form through the use of a timer.
I have tried multiple ways and have not been able to get it to work.
I hope someone here can help me with this.
private void SplashScreen_Load(object sender, EventArgs e)
{
splashScreenTimer.Enabled = true;
splashScreenTimer.Start();
splashScreenTimer.Interval = 1000;
progressBar.Maximum = 100;
splashScreenTimer.Tick += new EventHandler(timer1_Tick);
}
private void timer_Tick(object sender, EventArgs e)
{
if (progressBar.Value != 10)
{
progressBar.Value++;
}
else
{
splashScreenTimer.Stop();
}
}
you are assigning event_handler like
splashScreenTimer.Tick += new EventHandler(timer1_Tick);
and you are changing the progressBar value in
private void timer_Tick(object sender, EventArgs e)
{
if (progressBar.Value != 10)
{
progressBar.Value++;
}
else
{
splashScreenTimer.Stop();
}
}
change event handler to
splashScreenTimer.Tick += new EventHandler(timer_Tick);
or move codes to the other event handler timer1_Tick which should be in your form
For running the progressBar full in 4 seconds you can do like this
private void Form1_Load(object sender, EventArgs e)
{
splashScreenTimer.Enabled = true;
splashScreenTimer.Start();
splashScreenTimer.Interval = 30;
progressBar.Maximum = 100;
splashScreenTimer.Tick += new EventHandler(timer_Tick);
}
int waitingTime = 0;
private void timer_Tick(object sender, EventArgs e)
{
if (progressBar.Value < 100)
{
progressBar.Value++;
}
else
{
if (waitingTime++ > 35)
this.Close();
}
}
I am trying to make a program with 2 timers in it running at different intervals. Currently I have 1 timer working fine and I need to have another one running. My code for the first timer that works looks like this:
private void startButton_Click(object sender, RoutedEventArgs e)
{
Random rand = new Random();
int ranMin = rand.Next(1,24);
int ranSec = rand.Next(0, 59);
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, ranMin, ranSec);
dispatcherTimer.Start();
min.Content = ranMin;
sec.Content = ranSec;
openP();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
**code for timer in here
}
This works fine, but now I need another timer running at a 1second interval with different code and when I try to duplicate this by just making all the dispatcherTimer into dispatcherTimer2 I am running into errors.
I am not sure what you were doing (you should post the error), but the following works with your supplied code:
private void startButton_Click(object sender, RoutedEventArgs e)
{
Random rand = new Random();
int ranMin = rand.Next(1,24);
int ranSec = rand.Next(0, 59);
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, ranMin, ranSec);
dispatcherTimer.Start();
// New timer
System.Windows.Threading.DispatcherTimer dispatcherTimer2 = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer2.Tick += new EventHandler(dispatcherTimer2_Tick);
dispatcherTimer2.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer2.Start();
min.Content = ranMin;
sec.Content = ranSec;
openP();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
//code for timer in here
}
private void dispatcherTimer2_Tick(object sender, EventArgs e)
{
//code for timer2 in here
}