处理动态(运行时)控件的事件 - VB.NET

发布于 2024-07-13 09:48:35 字数 76 浏览 7 评论 0原文

我有一个在运行时创建并添加到窗体中的 WebBrowser 控件。

如何将此控件连接到可以在运行时处理其事件的子例程?

I have a WebBrowser control that is created and added to the form during run-time.

How do I connect this control to subroutine that can handle its events at run-time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

迷途知返 2024-07-20 09:48:36

示例

AddHandler SharedTimer.Tick、AddressOf SharedTimer_Tick

Example

AddHandler SharedTimer.Tick, AddressOf SharedTimer_Tick

空城之時有危險 2024-07-20 09:48:36

'我有一种方法可以发现控件并在某些情况下添加处理程序。
'这是一个简化的示例。
'是否可以在运行时传入处理程序?

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

“我想做一些更像这样的事情......

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

我遇到的问题是调用该方法。 这不起作用:

Example(myPlaceholder, addressof MyRuntimeHandler)

'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)
倒数 2024-07-20 09:48:36

您可以使用 Addhandler 语句来执行这些操作。
您可以在运行时将任何事件处理程序添加到网络浏览器,如下所示

AddHandler WebBrowser1.xEvent, AddressOf WebBrowser1EventHandler

,类似地,您可以使用RemoveHandler,它会断开事件与事件处理程序的连接,如下所示:

RemoveHandler WebBrowser1.XEvent, AddressOf WebBrowser1EventHandler

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 WebBrowser1.xEvent, AddressOf WebBrowser1EventHandler

and similarly you can use RemoveHandler, which disconnects an event from an event handler like this:

RemoveHandler WebBrowser1.XEvent, AddressOf WebBrowser1EventHandler
用心笑 2024-07-20 09:48:35

请使用 AddHandler

例如

AddHandler Obj.Ev_Event, AddressOf EventHandler

,当您想摆脱时, 根据

RemoveHandler Obj.Ev_Event, AddressOf EventHandler

在您的情况下,您可能会

Dim web as New WebBrowser()
AddHandler web.DocumentCompleted, AddressOf HandleDocumentCompleted

假设您创建了一个名为 HandleDocumentCompleted 的事件处理程序,

您的需要,您还可以使用 WithEvents 关键字,当您声明您的网络浏览器时; 请参阅文档

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

Depending on your needs, you could also use the WithEvents keyword when you declare your webbrowser; see the documentation.

云仙小弟 2024-07-20 09:48:35

使用 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

这种方法主要有两个优点:

  • 不需要 RemoveHandler
  • 不需要连接所有事件手动处理程序:这是自动完成的。

An alternative to using AddHandler is the declarative events syntax in VB. To use it, you declare the control (as a private member), using the WithEvents keyword. Then, the Handles keyword can be used on methods to handle the appropriate events:

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.
鱼窥荷 2024-07-20 09:48:35
  • 您将需要使用AddHandler 和RemoveHandler。
  • 如果您通过 AddHandler 手动添加事件,请务必使用 RemoveHandler 将其删除(在适当的位置)。
  • 键入“AddHandler NameOfControl”。 将通过智能感知给出可用事件的列表。
  • 智能感知、文档(或“错误列表”)还将为您提供事件处理程序的“签名”。

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
  • 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.

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
寒江雪… 2024-07-20 09:48:35

我通过检查表单设计器生成的代码了解到了这一点。 从那里复制一个示例,如果您环顾四周,您可能会学到一些有关在运行时设置控件的其他有价值的东西。

在 C# 中,它是通过 += 完成的,在以函数作为参数的类的事件成员上,但我没有方便地检查自己的 VB.net...抱歉。

编辑:它是 AddHandler 正如 Daniel L 在他的回答中所描述的那样,详细信息请参见 msdn

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.

EDIT: It's AddHandler as described well by Daniel L in his answer, and in full detail at msdn.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文