C# 处理动态(运行时)控件的事件 - VB.NET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/542724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Handle events for dynamic (run-time) controls - VB.NET
提问by user57175
I have a WebBrowser control that is created and added to the form during run-time.
我有一个在运行时创建并添加到表单的 WebBrowser 控件。
How do I connect this control to subroutine that can handle its events at run-time?
如何将此控件连接到可以在运行时处理其事件的子程序?
采纳答案by Daniel LeCheminant
Use AddHandler
e.g.
例如
AddHandler Obj.Ev_Event, AddressOf EventHandler
and when you want to get rid of it (and you should get rid of it when you're done using it)
当你想摆脱它时(当你用完它时你应该摆脱它)
RemoveHandler Obj.Ev_Event, AddressOf EventHandler
in your case, you might have something like
在你的情况下,你可能有类似的东西
Dim web as New WebBrowser()
AddHandler web.DocumentCompleted, AddressOf HandleDocumentCompleted
assuming you'd created an event handler called HandleDocumentCompleted
假设您创建了一个名为 HandleDocumentCompleted 的事件处理程序
Depending on your needs, you could also use the WithEventskeyword when you declare your webbrowser; see the documentation.
根据您的需要,您还可以在声明网络浏览器时使用WithEvents关键字;请参阅文档。
回答by jheriko
I learned about this from examining the Form Designer generated code. Copy one of the examples from there, and if you look around you might learn some other valuable things about setting up controls at run-time.
我通过检查表单设计器生成的代码了解到这一点。从那里复制一个例子,如果你环顾四周,你可能会学到一些关于在运行时设置控件的其他有价值的东西。
In C# its done with +=, on an event member of a class with a function as the parameter, but I do not have VB.net handy to check myself... sorry.
在 C# 中,它使用 += 完成,在类的事件成员上使用函数作为参数,但我没有 VB.net 方便检查自己......抱歉。
EDIT: It's AddHandleras described well by Daniel L in his answer, and in full detail at msdn.
编辑:它是AddHandler,正如 Daniel L 在他的回答中所描述的那样,并在msdn 上进行了详细描述。
回答by user62572
- You will need to use AddHandler and RemoveHandler.
- If you manually add an event through AddHandler be sure to remove it (in an appropriate location) using RemoveHandler.
- Typing "AddHandler NameOfControl." will give a list of available events via intellisense.
- Intellisense, documentation, (or the "error list"), will also give you the "signature" of the event handler.
- 您将需要使用 AddHandler 和 RemoveHandler。
- 如果您通过 AddHandler 手动添加事件,请确保使用 RemoveHandler 将其删除(在适当的位置)。
- 键入“AddHandler NameOfControl”。将通过智能感知提供可用事件列表。
- Intellisense、文档(或“错误列表”)也将为您提供事件处理程序的“签名”。
Private Sub WebBrowser1_Navigate(ByVal sender As Object, ByVal e As WebBrowserNavigatedEventArgs)
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
RemoveHandler WebBrowser1.Navigated, AddressOf WebBrowser1_Navigate
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler WebBrowser1.Navigated, AddressOf WebBrowser1_Navigate
End Sub
回答by Konrad Rudolph
An alternative to using AddHandler
is the declarative events syntax in VB. To use it, you declarethe control (as a private member), using the WithEvents
keyword. Then, the Handles
keyword can be used on methods to handle the appropriate events:
使用的替代方法AddHandler
是 VB 中的声明性事件语法。要使用它,请使用关键字声明控件(作为私有成员)WithEvents
。然后,Handles
可以在方法上使用关键字来处理适当的事件:
Private WithEvents m_WebBrowser As WebBrowser
Private Sub WebBrowser_Navigate(ByVal sender As Object, ByVal e As WebBrowserNavigatedEventArgs) Handles m_WebBrowser.Navigate
MsgBox("Hi there")
End Sub
Private Sub SomeActionThatCreatesTheControl()
m_WebBrowser = New WebBrowser()
End Sub
There are mainly two advantages to this method:
这种方法主要有两个优点:
- No need for
RemoveHandler
, - No need to wire all event handlers manually: this is done automatically.
- 不需要
RemoveHandler
, - 无需手动连接所有事件处理程序:这是自动完成的。
回答by Chris
Example
例子
AddHandler SharedTimer.Tick, AddressOf SharedTimer_Tick
AddHandler SharedTimer.Tick, AddressOf SharedTimer_Tick
回答by ben
'I have a method that discovers controls and adds handlers in certain situations.
'Here is a simplified example.
'Is it possible to pass in the handler at run time?
'我有一种方法可以在某些情况下发现控件并添加处理程序。
'这是一个简化的例子。
'是否可以在运行时传入处理程序?
Private Sub Example(byval ph as Placeholder)
for each ctrl as control in ph.controls
if typeof (ctrl) is textbox then
dim cb as checkbox = ctrl
AddHandler cb.DataBinding, AddressOf MyHandler
end if
next
end sub
'I'm looking to do something more like this...
“我想做一些更像这样的事情......
Private Sub Example(byval ph as Placeholder, **byref method as delagate**)
for each ctrl as control in ph.controls
if typeof (ctrl) is textbox then
dim cb as checkbox = ctrl
AddHandler cb.DataBinding, **method**
end if
next
end sub
The problem I'm having is in calling the method. This does not work:
我遇到的问题是调用方法。这不起作用:
Example(myPlaceholder, addressof MyRuntimeHandler)
回答by herry
You can use the Addhandler statement for doing these things. You can add any event handlers at run time to the webbrowser like this
您可以使用 Addhandler 语句来执行这些操作。您可以像这样在运行时向浏览器添加任何事件处理程序
AddHandler WebBrowser1.xEvent, AddressOf WebBrowser1EventHandler
and similarly you can use RemoveHandler, which disconnects an event from an event handler like this:
同样,您可以使用 RemoveHandler,它将事件与事件处理程序断开连接,如下所示:
RemoveHandler WebBrowser1.XEvent, AddressOf WebBrowser1EventHandler