第四部分
原文:
Timer Control
In VB6,setting an interval of 0 disables the timer. The new Timer control doesn't accept a value of 0 (it generates a runtime error) and can only be disabled by setting the Enable property to false. Code that doesn't use an interval of 0 to disable the timer will convert correctly. Code that switches between an interval of 0 (disabled) and a valid value can easily be corrected before converting. More complex code that uses both the Enable and the Interval properties in an "OR'ed" manner may be tricky to upgrade. One instance in which I dealt with this was a VB6 timer controlling a flashing error light. Any one of several subroutines could start the flashing by setting the enabled property to true,which was reset to false after user acknowledgement. The interval property was set to zero to globally disable the alarm. I rewrote the code to use an alarm object that handled all the logic. However,another option would be to write a new Timer class - derived from the .NET Timer class - that overrides the Enable and Interval properties and duplicates the behavior of the VB6 Timer.
Imports System Namespace VB6Timer Public Class VB6Timera Inherits System.Windows.Forms.Timer Private MyInterval As Integer Private MyEnabled As Boolean Public Sub New() MyInterval = MyBase.Interval MyEnabled = MyBase.Enabled End Sub Public Overloads Overrides Property Enabled() As Boolean Get Return MyEnabled End Get Set(ByVal Value As Boolean) MyEnabled = Value If Not Value Then MyBase.Enabled = False Else If Not (MyInterval = 0) Then MyBase.Enabled = True End If End If End Set End Property Public Shadows Property Interval() As Integer Get Return MyInterval End Get Set(ByVal Value As Integer) If Value = 0 Then MyBase.Enabled = False Else MyBase.Interval = Value If (MyInterval = 0) And (MyEnabled) Then MyBase.Enabled = True End If End If MyInterval = Value End Set End Property Protected Overloads Overrides Sub Dispose (ByVal disposing As Boolean) MyBase.Dispose(disposing) End Sub End Class End Namespace
To use this class,compile it under .NET and add a reference to it by browsing for it from the toolBox add component dialog. After the conversion,go to the formdesigner and remove the standard Timer,then add the VB6Timer from the toolBox to the form,and set its properties and name to match the old Timer control. This technique can be used in cases where there are minor differences between a VB6 control and its VB.NET replacement.
译文:
Timer 控件
在VB6里面,可以设置timer控件的interval为0来关闭定时器。而新的Timer(.NET中的)控件是不允许一个零值的(运行时会产生一个错误),但是你可以通过设置Enable属性为FALSE来关闭定时器。因此,不使用零值来关闭定时器的代码将会被正确的转换。在零值和有效值之间切换的代码可以在转换之前被很容易的纠正,但是更复杂的代码像在一个或运算中使用了Enable和Inverval属性的升级很可能会很棘手。我在一个instance中为了用一个VB6 Timer来控制一个错误指示灯闪烁会向上面所说的那样写代码。几个Sub中的任何一个都可以通过设置Enable属性来启动闪烁,如果可以的话用户知道后也可以将这个属性重置为false。其Interval属性可以设置为0来全局关闭Alarm。我重写代码来使用一个处理所有逻辑的Alarm。然而,还有另一个选择,就是写一个来自.NET Timer类的新的Timer类,以此来覆写Enable和Interval属性,同时还可以重构VB6 timer的行为。
Imports System Namespace VB6Timer Public Class VB6Timera Inherits System.Windows.Forms.Timer Private MyInterval As Integer Private MyEnabled As Boolean Public Sub New() MyInterval = MyBase.Interval MyEnabled = MyBase.Enabled End Sub Public Overloads Overrides Property Enabled() As Boolean Get Return MyEnabled End Get Set(ByVal Value As Boolean) MyEnabled = Value If Not Value Then MyBase.Enabled = False Else If Not (MyInterval = 0) Then MyBase.Enabled = True End If End If End Set End Property Public Shadows Property Interval() As Integer Get Return MyInterval End Get Set(ByVal Value As Integer) If Value = 0 Then MyBase.Enabled = False Else MyBase.Interval = Value If (MyInterval = 0) And (MyEnabled) Then MyBase.Enabled = True End If End If MyInterval = Value End Set End Property Protected Overloads Overrides Sub Dispose (ByVal disposing As Boolean) MyBase.Dispose(disposing) End Sub End Class End Namespace
要使用这个类,得先在.NET下编译,然后通过在工具箱和组件对话框中浏览来添加引用。在转换之后,在窗体设计器里面将标准的Timer控件移除,然后从工具箱添加VB6Timer到窗体里,然后修正其属性以及名称来和原先的Timer控件匹配。这个方法可以用在在VB6控件和VB.NET替代控件之间仅存在小小的区别的情况下。
下集预告
接下来我会写两篇文章,我们将会完成一些一般的(通用的)转换--ADO,ASP以及VB.NET 至C#,并且在VB.NET2005的升级向导中进行快速查找(问题等)。
原文链接:https://www.f2er.com/vb/259022.html