2

VB.NETのイベントをシャドウ/オーバーライドしたい。

これを行うのは、このオブジェクトに、ベースオブジェクト自体と同じイベントを含むインターフェイスを実装する必要があるためです。また、このベースイベントを、変更や補足イベントの追加なしでそのまま維持する必要があります。

どうすればいいですか?

Public Shadows Event VisibleChanged As EventHandler Implements IVisibleChanged

したがって、VisibleChangedイベントを含むインターフェイスを実装したいと思いますが、myBaseVisibleChangedイベントも機能し続けます。

  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

このようなものですが、VisualStudioはそのような構文を認識していないようです...

つまり、C#では次のように認識しています。

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

4 に答える 4

1
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
于 2009-11-27T10:29:39.960 に答える
0

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
于 2009-11-25T18:47:40.727 に答える
0
  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; }
}
于 2009-11-25T18:20:05.570 に答える
0

派生クラスでハンドラーをオーバーライドできますか?

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
于 2009-11-25T21:28:29.547 に答える