2009-11-25 77 views
2

我想在VB.NET中隱藏/覆蓋事件。在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; } 
} 

回答

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 
+0

這正是我需要的。謝謝。 – Kramii 2010-09-02 11:04:56

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#我reali它是這樣的:

public new event EventHandler VisibleChanged 
{ 
    add { base.Click += value; } 
    remove { base.Click -= value; } 
} 
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事件,他們也將能夠獲得ch到IFooBar:

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

我重新(編輯)我的問題線索。希望這會使這個主題更加清晰。 – serhio 2009-11-26 14:29:09

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 
+0

我只使用override **,因爲接口的實現需要這樣做。我根本不會改變*事件的行爲。 – serhio 2009-11-26 11:24:50