出现&使用计时器控件消失图像 [英] appear & disappear images using timer control

查看:77
本文介绍了出现&使用计时器控件消失图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3张图片(图片1,图片2,图片3),

使用间隔值为5000(即5秒,I
)的计时器控件 要完成以下任务:

-最初,用户只能在表单中间看到Image1,即2张图像
设置为不可见.
-加载表单时出现image1
出现在表格的正中间
-5秒钟后,图像1消失,图像2出现在右侧.
-5秒钟后,image2消失而image3出现在左侧
-5秒钟后,image3消失,image1出现

我在设计中将计时器控件的Enable属性设置为"True"
时间.
我将计时器控件的Interval属性值设置为5000 in
设计时间.

我假设我已完成所有编码,但仅显示两个图像而不工作


I have 3 Images (Image1, Image2, Image3),

Using a timer control with interval value of 5000, i.e 5second, I
want to accomplish the following:

- Initially, only Image1 is visible to the user middle of the form, i.e the 2 images
are set to be invisible.
- When load the form appear and image1
appears exact middle of the form
- After 5 seconds, the image1 disappear and image 2 appears right side.
- After 5 seconds, the image2 disappear and image3 appears left side
- After 5 seconds, the image3 disappear and the image1 appear

I am setting timer control''s Enable property to "True" in Design
time.
I am setting timer control''s Interval property value to 5000 in
Design Time.

I assume that I do all the coding but not working only displaying two images


Can anyone help me code what I want to accomplish?

推荐答案

如果从事件System.Windows.Forms.Timer.Tick的事件处理程序中调用Shahin所示的过程,则可以正常工作.假设这是一个称为SwitchImageVisibility的方法.现在,此计时器存在一个问题,即它的准确性令人难以置信.如果它对您有用,请使用它,但我警告您:错误可能非常严重,以至于用户可能清楚地看到计时器行为不是周期性的.

缺乏准确性可能令人难以置信.缺乏周期性的行为可以用徒手的眼睛看到并且非常刺激.我建议在精度无关紧要的极少数情况下使用此计时器,而这不是您的情况. (在这种情况下,可能会出现这样的随机延迟:仅事件被处理一次时,便会出现闪屏或类似的情况.)

如果使用System.Threading.Thread.Sleep(5000)创建具有周期性行为和延迟的单独线程,那就更好了.另一种方法是使用更精确的计时器System.Threading.TimerSystem.Timers.Timer.请参阅:
http://msdn.microsoft.com/en-us/library/system.threading. thread.aspx [^ ],
http://msdn.microsoft.com/en-us/library/system.threading. timer.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.timers. timer.aspx [ ^ ].

但是总的来说,使用线程代替计时器更安全,更简单.

System.Windows.Forms.Timer的唯一好处是,您可以从其Tick处理程序中调用UI方法/属性,这与所有其他情况不同.

您不能从非UI线程调用与UI相关的任何内容(其他计时器也可以从其他线程运行).相反,您需要使用System.Windows.Threading.DispatcherInvokeBeginInvoke方法(对于Forms或WPF)或System.Windows.Forms.Control(仅对于Forms).

在我过去的答案中,您将找到有关其工作原理的详细说明和代码示例:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview扫描仪和MD5的问题 [
The procedure Shahin showed would work correctly if called from the event handler of the event System.Windows.Forms.Timer.Tick. Let''s say, this is a method called SwitchImageVisibility. Now, a problem with this timer is that it is incredibly inaccurate. If it''s fine for your purpose, use it, but I warn it: the errors could be so bad that a user may clearly see that a timer behavior is not periodic.

This lack of accuracy can be unbelievably bad. The lack of periodic behavior can be visible with unarmed eye and very irritating. I would recommend to use this timer in rare cases when accuracy does not matter, and this is not your case. (One case when such a random delay is expected: flash screen or something like that, when the event is handled only once.)

If you create a separate thread with periodic behavior and a delay using System.Threading.Thread.Sleep(5000), it would be much better. Another approach would be using much more accurate timers, System.Threading.Timer or System.Timers.Timer. Please see:
http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx[^],
http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx[^].

But overall, using a thread instead of timer is safer and simpler.

The only benefit of System.Windows.Forms.Timer is that you can call UI methods/properties from its Tick handler, unlike all other cases.

You cannot call anything related to UI from non-UI thread (and other timers will work from a different thread, too). Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

So in your case, if you want to call SwitchImageVisibility in a method defined in a form or a control class, but in a non-UI thread, it will look like this:

this.Invoke(new Action(() => { SwitchImageVisibility(); }), null);



另请参阅有关线程的更多参考资料:
如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

-SA



See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


如果使用WinForm,则在Timer.Tick事件中:

If you use WinForm, then In Timer.Tick event:

if(Image1.Visible == true)
{
   Image1.Visible = false;
   Image2.Visible = true;
   return;
}
if(Image2.Visible == true)
{
   Image2.Visible = false;
   Image3.Visible = true;
   return;
}
if(Image3.Visible == true)
{
   Image3.Visible = false;
   Image1.Visible = true;
   return;
}


这篇关于出现&使用计时器控件消失图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆