我的模块1中有个多媒体计时器,多媒体计时器计时的同时,在窗体1里textbox显示记录的时间,但是调用textbox没有,现在在模块里使用委托也不行,哪位高人看看是哪里出错了,代码应该怎么改 ,希望高人帮帮忙
Public Class Form1
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'结束计时
timeKillEvent(SPtime)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'开始计时
mCount = 0
mTimerCallback = AddressOf CALLBACK_Timer
SPtime = timeSetEvent(10, 0, mTimerCallback, 0, 1)
End Sub
End Class
Module Module1
Public Delegate Sub TimerEventHandler(ByVal uID As UInt32,
ByVal uMsg As UInt32,
ByRef dwUser As UInt32,
ByVal dw1 As UInt32,
ByVal dw2 As UInt32)
Public Declare Function timeSetEvent Lib "winmm.dll" (ByVal uDelay As UInt32,
ByVal uResolution As UInt32,
ByVal lpFunction As TimerEventHandler,
ByRef dwUser As UInt32,
ByVal uFlags As UInt32) As UInt32
Public Declare Function timeKillEvent Lib "winmm.dll" (ByVal TimerID As Long) As Long '多媒体定时器关闭函数
Public Delegate Sub myDelegate(ByVal myString As UInt32) '自定义一个委托
Public Sub SetWindowText(ByVal myString As UInt32) '与委托相同签名的函数或过程
'Me.TextBox1.Text = myString
Form1.TextBox1.Text = myString
End Sub
Public mCount As UInt32
Public SPtime As UInt32
Public mTimerCallback As TimerEventHandler
Public Sub CALLBACK_Timer(ByVal uID As UInt32,
ByVal uMsg As UInt32,
ByRef dwUser As UInt32,
ByVal dw1 As UInt32,
ByVal dw2 As UInt32)
mCount = mCount + 1
Dim deli As New myDelegate(AddressOf SetWindowText)
deli.Invoke(mCount)
' Me.Invoke(New myDelegate(AddressOf SetWindowText), mCount)
End Sub
End Module
------解决思路----------------------
您的问题如下`这样解决了。
您的错误原因是您没有安全`调用`控件。
另外`一个问题是关闭定时器API函数中的参数类型有错误,我帮您改了。就这些
Class form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'开始计时
Module1._TextBox1 = Me.TextBox1
mCount = 0
mTimerCallback = AddressOf CALLBACK_Timer
SPtime = timeSetEvent(10, 0, mTimerCallback, 0, 1)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'结束计时
Module1._TextBox1 = Me.TextBox1
timeKillEvent(SPtime)
End Sub
End Class
Module Module1
Public Delegate Sub TimerEventHandler(ByVal uID As UInt32,
ByVal uMsg As UInt32,
ByRef dwUser As UInt32,
ByVal dw1 As UInt32,
ByVal dw2 As UInt32)
Public Declare Function timeSetEvent Lib "winmm.dll" (ByVal uDelay As UInt32,
ByVal uResolution As UInt32,
ByVal lpFunction As TimerEventHandler,
ByRef dwUser As UInt32,
ByVal uFlags As UInt32) As UInt32
Public Declare Function timeKillEvent Lib "winmm.dll" (ByVal TimerID As UInt32) As Long '多媒体定时器关闭函数
Public Delegate Sub myDelegate(ByVal myString As UInt32) '自定义一个委托
Private TextBox1 As TextBox
Public Property _TextBox1() As TextBox
Get
Return TextBox1
End Get
Set(ByVal value As TextBox)
TextBox1 = value
End Set
End Property
Public Sub SetWindowText(ByVal myString As UInt32) '与委托相同签名的函数或过程
'Me.TextBox1.Text = myString
If TextBox1.InvokeRequired Then
Dim S As New myDelegate(AddressOf SetWindowText)
TextBox1.Invoke(S, New Object() {[myString]})
Else
TextBox1.Text = myString
End If
End Sub
Public mCount As UInt32
Public SPtime As UInt32
Public mTimerCallback As TimerEventHandler
Public Sub CALLBACK_Timer(ByVal uID As UInt32,
ByVal uMsg As UInt32,
ByRef dwUser As UInt32,
ByVal dw1 As UInt32,
ByVal dw2 As UInt32)
mCount = mCount + 1
Dim deli As New myDelegate(AddressOf SetWindowText)
deli.Invoke(mCount)
' Me.Invoke(New myDelegate(AddressOf SetWindowText), mCount)
End Sub
End Module