Daniel Turner
Daniel Turner

Reputation: 139

is there a way to stop a timer on visual studios at a certain point instead of it keep going

basically I mean I want a picture to move when I press a button and the picture to sop moving at a certain point on the form application, not cancel it the picture to move then just stop with the application still running. on Microsoft visual studios c# windows form application

  using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;


        }
 private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
    private void Form1_Load(object sender, EventArgs e)
        {

        }
 private void timer1_Tick(object sender, EventArgs e)
        {
            pictureBox1.Left += 2;
        }
    }
}

Upvotes: 0

Views: 1099

Answers (1)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

You can call timer1.Stop() to stop the timer.

You need to set the Interval property of the timer to raise the TickEvent once after that Interval is Elapsed.

In TickEvent you can Stop the Timer or do whatever you want to do.

Try This :

private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval=5000;//to raise tick event for 5 sec's
timer1.Start();
timer1.Tick += new System.EventHandler(this.timer1_Tick);
}

protected void timer1_Tick(Object sender,EventArgs e)
{    
//Stop the timer here
timer1.Stop();    
}

Upvotes: 1

Related Questions