C#如何控制timer控件的开启与关闭

powerchuangwai 2009-11-04 09:40:50
该控件的启动与关闭分别是.start(),.stop(),可是我在用的时候有点问题。具体如下:

textBox_textChanged()
{
timer.start();
}
上述代码目的是实现textBox接收到数据后启动timer

设置好timer的关联事件是button_click(),时间间隔interval是20秒,如下:
button_click()
{
textBox.Text = "";
timer.stop();
}

这样,textBox接收到数据后启动timer,20秒后清空textBox,并通过timer.stop()关闭timer,当textBox接收到数据后重新启动timer,反复循环。结果是textBox接收到数据后timer启动20秒后清空textBox,然后关闭timer,但是当textBox再次接收到数据后无法启动timer。这是为什么?

在属性中已经设置timer.enabled = true;

请高手指点。。。希望能给出具体代码。。。
...全文
4640 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
nyq1999 2009-11-05
  • 打赏
  • 举报
回复
控制TIMER运行还是停止,就是timer.Enabled =true or false;STOP同样是靠这种方式控制TIMER的
powerchuangwai 2009-11-05
  • 打赏
  • 举报
回复
我发的就是全部的代码呀。

再看看吧。谢谢大家!!
hhc123 2009-11-04
  • 打赏
  • 举报
回复

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

namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)
{

}

private void timer1_Tick(object sender, EventArgs e)
{
textBox1.Text = "";
timer1.Stop();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
timer1.Start();

}


}
}

出来吧
wuyq11 2009-11-04
  • 打赏
  • 举报
回复
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 1000;
aTimer.Enabled = true;
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{

}
有四种System.Threading.Timer
System.Windows.Forms.Timer
System.Timers.Timer
System.Windows.Threading.DispatcherTimer
使用那个
hhc123 2009-11-04
  • 打赏
  • 举报
回复

再来一次,就不相信显不出源代码的效果
hhc123 2009-11-04
  • 打赏
  • 举报
回复
我发了为么多个怎么样就是显视不出原代码的效果啊,晕倒
yuzhouhuo 2009-11-04
  • 打赏
  • 举报
回复
LZ应该把全部CODE贴上来.
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sky_rv 的回复:]
timer.Enable=true; 开启timer控件;

timer。Enable=false;timer控件失效
[/Quote]

^o^
hhc123 2009-11-04
  • 打赏
  • 举报
回复
private void timer1_Tick(object sender, EventArgs e)
{
textBox1.Text = "";
timer1.Stop();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
timer1.Start();

}
hhc123 2009-11-04
  • 打赏
  • 举报
回复
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)
{

}

private void timer1_Tick(object sender, EventArgs e)
{
textBox1.Text = "";
timer1.Stop();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
timer1.Start();

}


}
}
完全可以啊??????????????
zhujiazhi 2009-11-04
  • 打赏
  • 举报
回复
private Timer _timer;
public Form1()
{
InitializeComponent();
_timer = new Timer();
//_timer.Enabled = true;
_timer.Tick += new EventHandler(_timer_Tick);
_timer.Interval = 10000;
_timer.Enabled = false;
}
void _timer_Tick(object sender, EventArgs e)
{
textBox2.Text = "";
_timer.Stop();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
_timer.Start();
}

没发现问题的
hhc123 2009-11-04
  • 打赏
  • 举报
回复

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

namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)
{

}

private void timer1_Tick(object sender, EventArgs e)
{
textBox1.Text = "";
timer1.Stop();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
timer1.Start();

}

private void button1_Click(object sender, EventArgs e)
{
timer1.Stop();
textBox1.Text = "";
}
}
}
可以啊
sky_rv 2009-11-04
  • 打赏
  • 举报
回复
timer.Enable=true; 开启timer控件;

timer。Enable=false;timer控件失效
ajaxtop 2009-11-04
  • 打赏
  • 举报
回复
button_click()
{
textBox.Text = "";
timer.stop();
}


当这里清空时,会不会导致timer死锁
hhc123 2009-11-04
  • 打赏
  • 举报
回复
private void timer1_Tick(object sender, EventArgs e)
{

}这个你没有用上吗?
PandaIT 2009-11-04
  • 打赏
  • 举报
回复
更改按钮上的文字来判断当前的状态!!

tianzi_zc 2009-11-04
  • 打赏
  • 举报
回复
可以再加一个Timer (用来监视TextBox的值是否为空),用来控制第一个Timer

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧