无法获取vb.net中声明的事件

本文关键字:声明 事件 net 获取 vb | 更新日期: 2023-09-27 18:17:40

我已经将下面的c#代码转换为vb.net。

Issue无法获取在一个类中声明的事件以在另一个对象中访问。

详情如下;

c#代码如下:

public Dropdown(CheckedComboBox ccbParent)
{
   this.ccbParent = ccbParent;
   InitializeComponent();
   this.ShowInTaskbar = false;                      
   this.cclb.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.cclb_ItemCheck);
}
private void cclb_ItemCheck(object sender, ItemCheckEventArgs e)
{
   if (ccbParent.ItemCheck != null)
   {
      ccbParent.ItemCheck(sender, e);
   }
}
vb.net

 Public Class CheckedComboBox
Inherits ComboBox

Public Event ItemCheck As ItemCheckEventHandler   
....
End Class
Private cclb As CustomCheckedListBox
   Public Event ItemCheck As Windows.Forms.ItemCheckEventHandler
   Public Sub New(ByVal ccbParent As PlexisCheckedComboBox)
   MyBase.New()
   Me.ccbParent = ccbParent
   InitializeComponent()
   Me.ShowInTaskbar = False
   AddHandler cclb.ItemCheck, AddressOf cclb_ItemCheck
End Sub
Private Sub cclb_ItemCheck(ByVal sender As Object, ByVal e As Windows.Forms.ItemCheckEventArgs)
   If (Not (ccbParent.ItemCheck) Is Nothing) Then
      ccbParent.ItemCheck(sender, e)
   End If
End Sub 

在转换后的vb.net代码中,我得到以下行

中的错误
If (Not (ccbParent.ItemCheck) Is Nothing) Then
   ccbParent.ItemCheck(sender, e) 

请帮我解决这个问题。ItemCHeck无法访问。

无法获取vb.net中声明的事件

c#代码的直接翻译如下;

Public Sub New(pCCBParent As CheckedComboBox)
    Me.ccbParent = pCCBParent
    InitializeComponent()
    Me.ShowInTaskbar = False
    AddHandler Me.cclb.ItemCheck, Me.cclb_ItemCheck
End Sub
Private Sub cclb_ItemCheck(sender As Object, e As ItemCheckEventArgs)
    RaiseEvent ccbParent.ItemCheck(sender, e)
End Sub

请注意两种语言之间的差异——很少有事情是不能逐字翻译的。

我在外部声明了sub,如下所示。呼叫ccbParent.RaiseItemCheckEvent(sender,e)

 Public Sub RaiseItemCheckEvent(ByVal sender As Object, ByVal e As ItemCheckEventArgs)
    RaiseEvent ItemCheck(sender, e)
End Sub

感谢大家的评论

不能直接从派生类引发基类事件-请尝试以下操作:

Class CheckedComboBox
    Inherits ComboBox
    Public Event ItemCheck As Windows.Forms.ItemCheckEventHandler
    Protected Sub RaiseItemCheckEventFromDerivedClass(ByVal sender As Object, ByVal e As ItemCheckEventArgs)
        RaiseEvent ItemCheck(sender, e)
    End Sub
End Class
Class DropDown
    Inherits CheckedComboBox
    Private ccbParent As CheckedComboBox
    Private Sub New(ByVal ccbParent As CheckedComboBox)
        Me.ccbParent = ccbParent
        AddHandler ccbParent.ItemCheck, AddressOf cclb_ItemCheck
    End Sub
    Private Sub cclb_ItemCheck(ByVal sender As Object, ByVal e As ItemCheckEventArgs)
        RaiseItemCheckEventFromDerivedClass(sender, e)
    End Sub
End Class