有没有办法在某个点停止视觉工作室的计时器,而不是继续前进

时间:2013-12-09 06:25:27

标签: c# timer visual-studio-2013

基本上我的意思是我想要一张图片移动,当我按下一个按钮并且图片在表格应用程序的某个点移动时,不取消它移动的图片然后停止,应用程序仍在运行。在Microsoft visual studio 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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以致电timer1.Stop()停止计时器。

Intervaltimer之后,您需要将raise的{​​{1}}属性设置为TickEvent Interval

Elapsed TickEvent您可以Stop Timer或做您想做的任何事情。

试试这个:

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();    
}