问题描述
我在调用时遇到问题,我有一个像这样的类,而在另一个里面
Hi, i have problem with invoke i have one class something like and inside another one
Public Class Form1
...........
...........
...........
public class client
...........
Private Sub finish(ByVal message As String)
If Me.InvokeRequired Then
Me.Invoke(New send(AddressOf finish), message)
Else
Form1.textbox1.text = message
End If
End Sub
End Class
end class
.....
end class
这给了我错误;调用;不是....
的成员我不明白我在另一个类中如何使用invoke怎么了?
我得到了答案
那仅仅是因为您尚未实现一种称为Invoke的方法.我想这是故意的,因为您似乎认为类客户端继承了Control,该控件具有名为Invoke的方法.
因此,您将必须实现自己调用或从Control [^]继承.
同样,Form1.client.InvokeRequired
但是,当我在客户端类中放入继承控件"时,错误消失了,但是我似乎没有调用它.我想为文本框分配值,但是一旦添加继承控件",它就不会出现
答案
and it gives me error ;Invoke; is not a member of ....
I don''t understand what''s wrong how I can use invoke in another class ??
I got answer
That''s simply because you have not implemented a method called Invoke. I guess that is intentional as you seem to think your class client inherits Control which has a method called Invoke.
So you''ll have to either implement Invoke yourself or inherit from Control[^].
The same goes for Form1.client.InvokeRequired
However when I put in client class "inherits control" error goes away but I doesn''t seem for me to invoke. I want to assign value to textbox but it doesnt come up once i add "inherits control"
ANSWER
You have the finish method in class client? Move the code to Form1 because that is the Control that can determine InvokeRequired. It also seems that your class would not need to inherit from Control so it shouldn't.
You need to invoke when a thread wants to change the UI. The UI control function you call from the thread must be thread safe and would need to check if InvokeRequired and Invoke itself if needed.
Have a look here for more info:
Updating the UI from a thread - The simplest way[^]
抱歉,我一直在获取答案,但我只是不了解线程方面的新知识,可能有些人可能会举个例子给我.
Sorry i keep getting answers but i just dont understand am new on threading and invoke maybe some of you could jus give me example please.
推荐答案
Private Delegate Sub FinishDelegate(ByVal Message As String)
Private Sub finish(ByVal message As String)
If Me.InvokeRequired Then
Dim a(0) As Object
a(0) = message
Dim del As New FinishDelegate(AddressOf finish)
me.Invoke(del, a)
Else
me.textbox1.text = message
End If
End Sub
您需要确保将form1对象传递给客户端(它需要知道将调用finish子例程的form1对象).假设您已经将form1对象传递给了客户端对象(假设它存储在名为ParentForm的变量中),那么当您要调用finish例程时,您只需要以下代码:
You''ll need to make sure the form1 object is passed to the client (it needs to know the form1 object which it will call the finish subroutine). Assuming you''ve passed the form1 object to the client object (pretend it''s stored in a variable called ParentForm), you''ll just need the following code when you want to call the finish routine:
ParentForm.finish("New Textbox1 Text")
这篇关于在其他类中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!