gpt4 book ai didi

.net - VB.NET 中的重写事件

转载 作者:行者123 更新时间:2023-12-05 05:29:13 25 4
gpt4 key购买 nike

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

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

我该怎么做?

Public Shadows Event VisibleChanged As EventHandler Implements IVisibleChanged

所以,我想实现一个包含 VisibleChanged 事件的接口(interface),但也要保持 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; }
}

最佳答案

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

关于.net - VB.NET 中的重写事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1798858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com