我将如何以vb.net的形式停止所有计时器(How will i Stop all timers in a form vb.net)

编程入门 行业动态 更新时间:2024-10-09 09:21:08
我将如何以vb.net的形式停止所有计时器(How will i Stop all timers in a form vb.net)

我使用计时器(作为提醒)创建动态表单作为通知或警报表单。 我在每张表格上指定一个名字。

所以无论何时更新..我想关闭或禁用该特定表单上的计时器,以便它永远不会显示(作为警报)。

为每个控件找到计时器不起作用,我无法禁用它。

For Each f As Form In My.Application.OpenForms If (f.Name = Label10.Text) Or (f.Name = "notification" & Label9.Text) Then Dim timer = Me.components.Components.OfType(Of IComponent)().Where(Function(p) p.[GetType]().FullName = "System.Windows.Forms.Timer").ToList() For Each cmd In timer If Not cmd Is Nothing Then Dim tmp As Timer = DirectCast(cmd, Timer) tmp.Enabled = False tmp.Stop() End If Next End If Next

我将如何改变(Me.Components.Components)到f我的形式(f.Components.Components)请帮助我。

I create dynamic form with timer(as a reminder) as a notification or alert form. i assign a name on each form.

so whenever it is updated.. i want to close or to disable the timer on that certain form so it will never show (as an alert).

the for each control to find timer doesn't work, i can't disable it.

For Each f As Form In My.Application.OpenForms If (f.Name = Label10.Text) Or (f.Name = "notification" & Label9.Text) Then Dim timer = Me.components.Components.OfType(Of IComponent)().Where(Function(p) p.[GetType]().FullName = "System.Windows.Forms.Timer").ToList() For Each cmd In timer If Not cmd Is Nothing Then Dim tmp As Timer = DirectCast(cmd, Timer) tmp.Enabled = False tmp.Stop() End If Next End If Next

How will i change (Me.Components.Components) to f which is my form (f.Components.Components) please help me.

最满意答案

为了遍历表单上的计时器,您需要首先获取它们。 控件集合不包含任何计时器对象。 定时器是由microsoft用非托管C / C ++代码编写的,并且只有很少的包装器来支持它们在.NET中的API。

你仍然可以通过一点点的方式访问它们。 我已经测试了以下代码,它可以在表单上使用1个计时器。 我没有尝试超过1个计时器,但它应该工作。

Dim timer = Me.components.Components.OfType(Of IComponent)().Where(Function(p) p.[GetType]().FullName = "System.Windows.Forms.Timer").ToList() For Each cmd In timer If Not cmd Is Nothing Then Dim tmp As Timer = DirectCast(cmd, Timer) tmp.Enabled = False tmp.Stop() End If Next

这个代码的另一个版本可能看起来像这样,进行了一些LINQ优化:

Dim timer = Me.components.Components.OfType(Of IComponent)().Where(Function(ti) ti.GetType().FullName = "System.Windows.Forms.Timer").ToList() For Each tmp As Timer In (From cmd In timer Where Not cmd Is Nothing).Cast(Of Timer)() tmp.Enabled = False tmp.Stop() Next

In order to loop through the timers on the form you need to first get a hold of them. The controls collection does not contain any timer objects. Timers were written in unmanaged C/C++ code by microsoft and only have little wrappers to support their API in .NET.

You can still access them though through a bit of finagling. I have tested the following code and it does work with 1 timer on the form. I have not tried it with more than 1 timer, but it should work.

Dim timer = Me.components.Components.OfType(Of IComponent)().Where(Function(p) p.[GetType]().FullName = "System.Windows.Forms.Timer").ToList() For Each cmd In timer If Not cmd Is Nothing Then Dim tmp As Timer = DirectCast(cmd, Timer) tmp.Enabled = False tmp.Stop() End If Next

Another version of this code could look like this with a bit of LINQ optimization going on:

Dim timer = Me.components.Components.OfType(Of IComponent)().Where(Function(ti) ti.GetType().FullName = "System.Windows.Forms.Timer").ToList() For Each tmp As Timer In (From cmd In timer Where Not cmd Is Nothing).Cast(Of Timer)() tmp.Enabled = False tmp.Stop() Next

更多推荐

本文发布于:2023-08-04 00:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1402480.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:计时器   我将   形式   vb   form

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!