我使用计时器(作为提醒)创建动态表单作为通知或警报表单。 我在每张表格上指定一个名字。
所以无论何时更新..我想关闭或禁用该特定表单上的计时器,以便它永远不会显示(作为警报)。
为每个控件找到计时器不起作用,我无法禁用它。
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 NextHow 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() NextIn 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 NextAnother 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更多推荐
发布评论