我将如何停止所有计时器的形式vb.net [英] How will i Stop all timers in a form vb.net
问题描述
我使用计时器(作为提示)创建动态表单作为通知或警报表单。我为每一个表格分配一个名称。
所以每当它被更新..我想关闭或禁用定时器在某种形式,所以它永远不会显示(一个警报)。
为每个控件找到定时器不工作,我不能禁用它。
For Each f As Form 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]()。 ToList()
For Each cmd In timer
If Not cmd Is Nothing Then
Dim tmp As Timer = DirectCast(cmd,Timer)FullName =System.Windows.Forms.Timer
tmp.Enabled = False
tmp.Stop()
结束如果
下一个
结束如果
下一个
如何我会改变(Me.Components.Components)为f这是我的表单(f.Components.Components),请帮助我。
为了循环表单中的定时器,你需要首先获取它们。控件集合不包含任何计时器对象。定时器是由微软编写的非托管C / C ++代码,并且只有很少的包装来支持他们在.NET中的API。你仍然可以通过一些finagling访问它们。我测试了下面的代码,它在窗体上使用1个计时器。我还没有用超过1个计时器,但它应该工作。
昏暗定时器= Me.components.Components.OfType (Of IComponent)()。函数(p)p。[GetType]()。FullName =System.Windows.Forms.Timer)。ToList()
For Each cmd In timer
如果不是cmd就没有那么
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() ToList()
对于每个tmp As定时器在(从cmd在定时器其中不是cmd是没有).Cast(Of Timer)()
tmp .Enabled = False
tmp.Stop()
下一个
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.
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
这篇关于我将如何停止所有计时器的形式vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!