覆盖 VB.NET 中的事件

发布于 2024-08-12 15:07:39 字数 887 浏览 6 评论 0原文

我想隐藏/覆盖 VB.NET 中的事件。

我这样做只是因为我必须在此对象中实现一个接口,该接口包含与基础对象本身相同的事件,并且我需要保持此基础事件不变,无需修改,也无需添加补充事件。

我该怎么做呢?

Public Shadows Event VisibleChanged As EventHandler Implements IVisibleChanged

因此,我想实现一个包含 VisibleChanged 事件的接口,但也要保持 myBase VisibleChanged 事件的功能。

  Public Shadows Event VisibleChanged As EventHandler Implements IVisibleChanged
    AddHandler(ByVal value As EventHandler)
      AddHandler MyBase.VisibleChanged, value
    End AddHandler
    RemoveHandler(ByVal value As EventHandler)
      RemoveHandler MyBase.VisibleChanged, value
    End RemoveHandler
  End Event

像这样的东西,但 Visual Studio 似乎无法识别这样的语法...

我的意思是,在 C# 中我是这样实现的:

public new event EventHandler VisibleChanged
{
    add { base.VisibleChanged += value; }
    remove { base.VisibleChanged -= value; }
}

I would like to shadow/override an event in VB.NET.

I do it only because I have to implement in this object an interface, that contains the same event like the base object itself, and I need to keep this base event as is it without modification nor supplementary event additions.

How can I do it?

Public Shadows Event VisibleChanged As EventHandler Implements IVisibleChanged

So, I would like to implement a interface that contains VisibleChanged event, but to keep functional the myBase VisibleChanged event too.

  Public Shadows Event VisibleChanged As EventHandler Implements IVisibleChanged
    AddHandler(ByVal value As EventHandler)
      AddHandler MyBase.VisibleChanged, value
    End AddHandler
    RemoveHandler(ByVal value As EventHandler)
      RemoveHandler MyBase.VisibleChanged, value
    End RemoveHandler
  End Event

something like this, but it seems Visual Studio does not recognize such a syntax...

I mean, in C# I realize it like this:

public new event EventHandler VisibleChanged
{
    add { base.VisibleChanged += value; }
    remove { base.VisibleChanged -= value; }
}

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

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

发布评论

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

评论(4

平生欢 2024-08-19 15:07:40

您可以重写派生类中的处理程序吗?

Public Class OverriddenClass
Inherits OriginalClass
Protected Overrides Function ProcessVisibleChanged(ByRef msg as Message, value as ...) As Boolean
  value = value + 1
  ProcessVisibleChanged = MyBase.ProcessVisibleChanged(msg, value)
  ' or use OnVisibleChanged to avoid the base handler
End Function
end class

Can you override the handler in a derived class?

Public Class OverriddenClass
Inherits OriginalClass
Protected Overrides Function ProcessVisibleChanged(ByRef msg as Message, value as ...) As Boolean
  value = value + 1
  ProcessVisibleChanged = MyBase.ProcessVisibleChanged(msg, value)
  ' or use OnVisibleChanged to avoid the base handler
End Function
end class
美男兮 2024-08-19 15:07:40
  Public Shadows Event VisibleChanged As EventHandler Implements IVisibleChanged
    AddHandler(ByVal value As EventHandler)
      AddHandler MyBase.VisibleChanged, value
    End AddHandler
    RemoveHandler(ByVal value As EventHandler)
      RemoveHandler MyBase.VisibleChanged, value
    End RemoveHandler
  End Event

像这样的东西,但 Visual Studio 似乎无法识别这样的语法...

我的意思是,在 C# 中我是这样实现的:

public new event EventHandler VisibleChanged
{
    add { base.Click += value; }
    remove { base.Click -= value; }
}
  Public Shadows Event VisibleChanged As EventHandler Implements IVisibleChanged
    AddHandler(ByVal value As EventHandler)
      AddHandler MyBase.VisibleChanged, value
    End AddHandler
    RemoveHandler(ByVal value As EventHandler)
      RemoveHandler MyBase.VisibleChanged, value
    End RemoveHandler
  End Event

something like this, but it seems Visual Studio does not recognize such a syntax...

I mean, in C# I realize it like this:

public new event EventHandler VisibleChanged
{
    add { base.Click += value; }
    remove { base.Click -= value; }
}
若水般的淡然安静女子 2024-08-19 15:07:39
Option Strict On
Option Explicit On

Public Class Form1
  Public Sub New()
    ' This call is required by the Windows Form Designer.'
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.'
    Dim b As New MyButton
    Me.Controls.Add(b)
    b.Name = "customButton"
    b.Location = New Point(10, 10)
    b.Text = "Hit me!"
    AddHandler CType(b, IMyInterface).Click, AddressOf MyButton1_Click
  End Sub

  Private Sub MyButton1_Click( _ 
      ByVal sender As System.Object, ByVal e As System.EventArgs)
    Debug.Print("{0} clicked!; ", CType(sender, Control).Name)
  End Sub
End Class

' ------- interface'
Public Interface IMyInterface
  Event Click As EventHandler
End Interface

' ------- class'
Public Class MyButton
  Inherits System.Windows.Forms.Button
  Implements IMyInterface
  ' ============ HERE IS THE SOLUTION'
  Private Event Click1 As EventHandler Implements IMyInterface.Click

  Private Sub ResendClick( _ 
      ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Click
    RaiseEvent Click1(sender, e)
  End Sub
  ' END HERE IS THE SOLUTION ============ '
End Class
Option Strict On
Option Explicit On

Public Class Form1
  Public Sub New()
    ' This call is required by the Windows Form Designer.'
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.'
    Dim b As New MyButton
    Me.Controls.Add(b)
    b.Name = "customButton"
    b.Location = New Point(10, 10)
    b.Text = "Hit me!"
    AddHandler CType(b, IMyInterface).Click, AddressOf MyButton1_Click
  End Sub

  Private Sub MyButton1_Click( _ 
      ByVal sender As System.Object, ByVal e As System.EventArgs)
    Debug.Print("{0} clicked!; ", CType(sender, Control).Name)
  End Sub
End Class

' ------- interface'
Public Interface IMyInterface
  Event Click As EventHandler
End Interface

' ------- class'
Public Class MyButton
  Inherits System.Windows.Forms.Button
  Implements IMyInterface
  ' ============ HERE IS THE SOLUTION'
  Private Event Click1 As EventHandler Implements IMyInterface.Click

  Private Sub ResendClick( _ 
      ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Click
    RaiseEvent Click1(sender, e)
  End Sub
  ' END HERE IS THE SOLUTION ============ '
End Class
月下伊人醉 2024-08-19 15:07:39

在 VB.NET 中实现接口时,不需要将实现命名为与接口相同的名称。

接口示例:

Public Interface IFoo
    Event Bar As EventHandler
End Interface

实现示例:

Public Class FooImplementation
    Implements IFoo

    ' This is the built-in Bar event, specific to this class.
    Public Event Bar As EventHandler

    ' This is the implemented IFoo.Bar event.
    Public Event IFooBar As EventHandler Implements IFoo.Bar

End Class

当某人作为 IFoo 附加到您时,他们将获得您的 IFooBar 事件:

Dim instance As IFoo = New FooImplementation()
AddHandler instance.Bar, AddressOf IFooBarHandler

当他们作为 FooImplementation 附加到您时,他们将获得 Bar 事件,并且他们还能够附加到 IFooBar :

Dim instance As New FooImplementation()
AddHandler instance.Bar, AddressOf BarHandler
AddHandler instance.IFooBar, AddressOf IFooBarHandler

When implementing an interface in VB.NET, you don't need to name the implementation the same name as the interface.

Example interface:

Public Interface IFoo
    Event Bar As EventHandler
End Interface

Example implementation:

Public Class FooImplementation
    Implements IFoo

    ' This is the built-in Bar event, specific to this class.
    Public Event Bar As EventHandler

    ' This is the implemented IFoo.Bar event.
    Public Event IFooBar As EventHandler Implements IFoo.Bar

End Class

When somebody is attached to you as an IFoo, they will get your IFooBar event:

Dim instance As IFoo = New FooImplementation()
AddHandler instance.Bar, AddressOf IFooBarHandler

When they're attached to you as FooImplementation, they'll get the Bar event and they will also be able to attach to IFooBar:

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