① timer怎么用
Timer 控件方案:响应 Timer 事件
经历了 Timer 控件的时间间隔后,Visual Basic 将生成 Timer 事件。通常,在响应此事件时将检查某些一般条件,例如系统时钟。
数字型时钟是涉及 Timer 控件的简单而有用的应用程序。一旦理解此应用程序的工作方式,就可增强它的性能,实现闹钟、跑表或其它定时设备的功能。
Digital Clock 应用程序包含一个定时器和一个有边框的标签。
定时器在运行时不可见。下表列出了 Digital Clock 应用程序中的属性设置值。
控件 属性 设置值
Label1 BorderStyle Fixed Single
Timer1 Interval 500(半秒)
Timer1 Enabled True
应用程序中的唯一过程是定时器的事件过程:
Private Sub Timer1_Timer ()
If lblTime.Caption <> CStr(Time) Then
lblTime.Caption = Time
End If
End Sub
过程调用内在的 Time 函数来显示系统时间。此函数返回一个 Variant,以日期/时间数值 (VarType 7) 的形式包含当前时间。将它赋予某个字符串变量或属性时,比如赋予本例中的 Caption 属性时,Visual Basic 将用“控制面板”中指定的格式将其转换成一个字符串。若要用其它格式显示,可使用 Format 函数。
详细信息 请参阅“Format 属性”。
将定时器的 Interval 属性设置为 500,按照上述原则将 Interval 设置为要区分的最短时间(本例中为一秒钟)的一半。这会使 Timer 控件代码每半秒更新一次标签。这样做不仅浪费而且可能导致视觉颤动,因此代码在改变标题之前要检查当前时间是否与在标签上显示的时间不同。
无需另外编写语句就可自定义数字时钟的外观。例如,可为标签选择其它字体或改变窗体的 BorderStyle 属性。
② java 中timer类的用法是什么
现在项目中用到需要定时去检查文件是否更新的功能。timer正好用于此处。
用法很简单,new一个timer,然后写一个timertask的子类即可。
package comz.autoupdatefile;
import java.util.Timer;
import java.util.TimerTask;
public class M {
public static void main(String[] args) {
// TODO todo.generated by zoer
Timer timer = new Timer();
timer.schele(new MyTask(), 1000, 2000);
}
}
class MyTask extends TimerTask {
@Override
public void run() {
System.out.println("dddd");
}
}
这样,就可以在1秒钟之后开始执行mytask,每两秒钟执行一次。
当然,timer的功能也可以通过自己构造线程,然后在线程中用sleep来模拟停止一段时间,然后再执行某个动作。
其实,看一下timertask的源码就立即可以知道,timertask就是实现了runnable接口的。也就是说,通过timer来间隔一段时间执行一个操作,也是通过一个线程来做到的。
③ timer控件用法
Timer控件
Timer控件主要会用到2个属性一个是Enabled和Interval
Enabled主要是控制当前Timer控件是否可用
timer1.Enabled=false;不可用
timer1.Enabled=true;可用
timer1.Interval=1000;主要是设置timer2_Tick事件的时间,单位为毫秒
例一:到9:00提示去上厕所:(
把timer2.Interval=60000;//1分钟
private void timer2_Tick(object sender, System.EventArgs e){
string cesuotime=DateTime.Now.DateTime.Now.ToShortTimeString();//得到现在的时间
if(cesuotime.equles("9:00")){
timer1.Enabled=false;
MessageBox.show("该去上厕所了");
timer1.Enabled=true;//如果不先把enabled设置成false对话框会一直弹下去
}
}
例二:每2小时提示用户看电脑时间已经很久了,需要休息了
把timer2.Interval=7200000;//7200秒
private void timer2_Tick(object sender, System.EventArgs e){
timer1.Enabled=false;
MessageBox.show("需要休息了,开机已经2小时了");
timer1.Enabled=true;//如果不先把enabled设置成false对话框会一直弹下去
}
}
④ C# timer如何使用
privatevoidtimer1_Tick(objectsender,EventArgse)
{
if(textBox1.Text=="")
textBox1.Text="HelloWorld";
else
textBox1.Text="";
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
if(!timer1.Enabled)
{
timer1.Start();
timer1.Enabled=true;
}
else
{
timer1.Enabled=false;
timer1.Stop();
}
⑤ timer的用法
public Form1()
{
InitializeComponent();
}
这句不用理会
this.timer1.Interval = 1000;
设置timer1对象的事件触发间隔。
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
为timer1对象绑定事件,该处应用委托的方式,委托的定义网络一下就知道了。
private void timer1_Tick(object sender, EventArgs e)
{
if (time > -1)
{
time--;
this.label1.Text = time.ToString();
//this.label1.Refresh();
}
else
{
timer1.Stop();
}
}
上述委托方法的实现,原意让label1现实倒计时,由10递减,没见个1秒刷新显示。
⑥ NET中的三种Timer的区别和用法
在.NET1.1里面,第3个System.Timers.Timer,也是可以拖拽使用,而.NET2.0开始取消了,只能手动编写代码。而后2个没有限制制。操作方法如下:
1、首先使用RadTileList控件,TileRows="3" 三行,SelectionMode="Multiple" 多选。
⑦ c#如何使用timer
可以在窗体添加一个timer控件,或者自定义timer也可以
例如
Timer procTimer = new Timer();
然后注册timer事件
procTimer .Tick += new EventHandler(procTimer _Tick);
定义timer函数
void procTimer _Tick(object sender, EventArgs e)
{
。。。。。。
}
设置timer计数间隔
procTimer.Interval = 100;//默认100毫秒
启动timer
.procTimer .Start();
⑧ 新手问下timer怎么调用
C#中Timer组件用法 Timer组件是也是一个WinForm组件了,和其他的WinForm组件的最大区别是:Timer组件是不可见的,而其他大部分的组件都是都是可见的,可以设计的。Timer组件也被封装在名称空间System.Windows.Forms中,其主要作用是当Timer组件启动后,每隔一个固定时间段,触发相同的事件。Timer组件在程序设计中是一个比较常用的组件,虽然属性、事件都很少,但在有些地方使用它会产生意想不到的效果。
其实要使得程序的窗体飘动起来,其实思路是比较简单的。首先是当加载窗体的时候,给窗体设定一个显示的初始位置。然后通过在窗体中定义的二个Timer组件,其中一个叫Timer1,其作用是控制窗体从左往右飘动(当然如果你愿意,你也可以改为从上往下飘动,或者其他的飘动方式。),另外一个Timer2是控制窗体从右往左飘动(同样你也可以改为其他飘动方式)。当然这二个Timer组件不能同时启动,在本文的程序中,是先设定Timer1组件启动的,当此Timer1启动后,每隔0.01秒,都会在触发的事件中给窗体的左上角的横坐标都加上"1",这时我们看到的结果是窗体从左往右不断移动,当移动到一定的位置后,Timer1停止。Timer2启动,每隔0.01秒,在触发定义的事件中给窗体的左上角的横坐标都减去"1",这时我们看到的结果是窗体从右往左不断移动。当移动到一定位置后,Timer1启动,Timer2停止,如此反复,这样窗体也就飘动起来了。要实现上述思路,必须解决好以下问题。
(1).如何设定窗体的初始位置:
设定窗体的初始位置,是在事件Form1_Load()中进行的。此事件是当窗体加载的时候触发的。Form有一个DesktopLocation属性,这个属性是设定窗体的左上角的二维位置。在程序中是通过Point结构变量来设定此属性的值,具体如下:
//设定窗体起初飘动的位置,位置为屏幕的坐标的(0,240)
private void Form1_Load ( object sender , System.EventArgs e )
{
Point p = new Point ( 0 , 240 ) ;
this.DesktopLocation = p ;
}
(2). 如何实现窗体从左往右飘动:
设定Timer1的Interval值为"10",就是当Timer1启动后,每隔0.01秒触发的事件是Timer1_Tick(),在这个事件中编写给窗体左上角的横坐标不断加"1"的代码,就可以了,具体如下:
private void timer1_Tick(object sender, System.EventArgs e)
{
{ //窗体的左上角横坐标随着timer1不断加一
Point p = new Point ( this.DesktopLocation.X + 1 , this.DesktopLocation.Y ) ;
this.DesktopLocation = p ;
if ( p.X == 550 )
{
timer1.Enabled = false ;
timer2.Enabled = true ;
}
}
(3). 如何实现窗体从右往左飘动:
代码设计和从左往右飘动差不多,主要的区别是减"1"而不是加"1"了,具体如下:
//当窗体左上角位置的横坐标为-150时,timer2停止,timer1启动
private void timer2_Tick(object sender, System.EventArgs e)
{ file://窗体的左上角横坐标随着timer2不断减一
Point p = new Point ( this.DesktopLocation.X - 1 , this.DesktopLocation.Y ) ;
this.DesktopLocation = p ;
if ( p.X == - 150 )
{
timer1.Enabled = true ;
timer2.Enabled = false ;
}
}
三. 用Visual C#编写窗体飘动程序的源代码:
通过上面的介绍,不难写出窗体飘动的程序源代码。如下:
using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
namespace floatingForm
{
public class Form1 : Form
{
private Timer timer1 ;
private Timer timer2 ;
private Label label1 ;
private Button button1 ;
private System.ComponentModel.IContainer components ;
public Form1 ( )
{
file://初始化窗体中的各个组件
InitializeComponent ( ) ;
}
file://清除在程序中使用过的资源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose( disposing ) ;
}
private void InitializeComponent ( )
{
this.components = new System.ComponentModel.Container ( ) ;
this.timer1 = new Timer ( this.components ) ;
this.timer2 = new Timer ( this.components ) ;
this.label1 = new Label ( ) ;
this.button1 = new Button ( ) ;
this.SuspendLayout ( ) ;
this.timer1.Enabled = true ;
this.timer1.Interval = 10 ;
this.timer1.Tick += new System.EventHandler ( this.timer1_Tick ) ;
this.timer2.Enabled = false ;
this.timer2.Interval = 10 ;
this.timer2.Tick += new System.EventHandler ( this.timer2_Tick ) ;
this.button1.Font = new Font ( "宋体" , 10 ) ;
this.button1.Location = new Point ( 1 , 8 ) ;
this.button1.Name = "button1" ;
this.button1.Size = new Size ( 80 , 25 ) ;
this.button1.TabIndex = 0 ;
this.button1.Text = "停止飘动" ;
this.button1.Click += new System.EventHandler ( this.button1_Click ) ;
this.label1.Font = new Font ( "宋体" , 22F , FontStyle.Bold , GraphicsUnit.Point , ( ( System.Byte ) ( 0 ) ) ) ;
this.label1.Location = new Point ( 8 , 38 ) ;
this.label1.Name = "label1" ;
this.label1.Size = new Size ( 344 , 40 ) ;
this.label1.TabIndex = 1 ;
this.label1.Text = "用Visual C#做的飘动的窗体!" ;
this.AutoScaleBaseSize = new Size ( 5 , 13 ) ;
this.ClientSize = new Size ( 352 , 70 ) ;
this.Controls.Add (this.label1 ) ;
this.Controls.Add (this.button1 ) ;
this.Name = "Form1" ;
this.Text = "用Visual C#做的飘动的窗体!";
this.Load += new System.EventHandler ( this.Form1_Load ) ;
this.ResumeLayout ( false ) ;
}
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
file://设定窗体起初飘动的位置
private void Form1_Load ( object sender , System.EventArgs e )
{
Point p = new Point ( 0 , 240 ) ;
this.DesktopLocation = p ;
}
file://当窗体左上角位置的横坐标为550时,timer1停止,timer2启动
private void timer1_Tick(object sender, System.EventArgs e)
{
file://窗体的左上角横坐标随着timer1不断加一
Point p = new Point ( this.DesktopLocation.X + 1 , this.DesktopLocation.Y ) ;
this.DesktopLocation = p ;
if ( p.X == 550 )
{
timer1.Enabled = false ;
timer2.Enabled = true ;
}
}
file://当窗体左上角位置的横坐标为-150时,timer2停止,timer1启动
private void timer2_Tick(object sender, System.EventArgs e)
{ file://窗体的左上角横坐标随着timer2不断减一
Point p = new Point ( this.DesktopLocation.X - 1 , this.DesktopLocation.Y ) ;
this.DesktopLocation = p ;
if ( p.X == - 150 )
{
timer1.Enabled = true ;
timer2.Enabled = false ;
}
}
file://停止所有的timer
private void button1_Click(object sender, System.EventArgs e)
{
timer1.Stop ( ) ;
timer2.Stop ( ) ;
}
}
}
四. 总结:
恰到好处的使用Timer组件往往会有出其不意的效果。由于本文的主要目的是介绍Timer组件的使用方法,程序功能还不是十分强大,感兴趣的读者,可以试着按照下面的思路进行修改,看看是否可以让窗体上下飘动,让窗体不定规则的飘动。当然如果你更有兴趣,也可以把窗体的边框和最大化、最小化等按钮给隐去,放一个好看的图片充满整个窗体,再让他飘动起来,这样效果就更令人惊讶了。
⑨ vb中timer怎么用
简单的说Timer是一个时间的控件,不能说Timer的值等于2秒时,因为Timer相当于时间,只能说当Timer跳动两秒时触发某事件。
例如:
1、在窗体上添加一个timer控件,默认名称为Timer1。
2、将Timer1的Interval属性设置为1000,即每隔1000毫秒(1秒)触发一次。
3、在Timer1的Timer()事件中编写如下代码
PrivateSub Timer1_Timer()
Print Time
EndSub
4、运行该程序,窗体上每隔1秒就会输出一次系统当前时间。
(9)ctimer使用方法扩展阅读:
具体代码如下:
首先要确定启动控件
Me.Timer1.Enabled = True’启动控件
Me.Timer1.Interval = 1000‘设定跳动频为1秒。1000=1秒
dimsTime as string=0
然后在控件的Timer1.Tick事件下写下面的代码
sTime=stime+1
if sTime=2 then
'你所要触发事件的代码
msgbox("OK")
endif
参考资料:网络—C语言