从C#转换为VB错误

时间:2012-02-23 08:59:20

标签: c# vb.net delegates

我已经转换了一个将xls文件嵌入到Windows窗体应用程序到vb的工作C#类。

我收到错误:

  

“重载解决方案失败,因为没有可访问的'Navigate'接受此数量的参数”

在以下行:

从:

this.WebBrowserExcel.Navigated+=new System.Windows.Forms.WebBrowserNavigatedEventHandler(this.OnWebBrowserExcelNavigated);

为:

Me.WebBrowserExcel.Navigate += New System.Windows.Forms.WebBrowserNavigatedEventHandler(AddressOf Me.OnWebBrowserExcelNavigated)

我也尝试过:

Me.WebBrowserExcel.Navigate += New System.Windows.Forms.WebBrowserNavigatedEventHandler(Me.OnWebBrowserExcelNavigated)

但我收到错误:

  

错误13委托'System.Windows.Forms.WebBrowserNavigatedEventHandler'需要'AddressOf'表达式或lambda表达式作为其构造函数的唯一参数。

任何想法?

提前感谢。

2 个答案:

答案 0 :(得分:5)

C#delegate +=的直接翻译在VB中是AddHandler

AddHandler Me.WebBrowserExcel.Navigated, AddressOf Me.OnWebBrowserExcelNavigated

或者,您可以以VB.NET方式并使用WithEvents and the Handles Clause

Function OnWebBrowserExcelNavigated(...) Handles WebBrowserExcel.Navigate
    ...
End Function

答案 1 :(得分:2)

在vbnet中处理事件时,应使用AddHandler

试试这个:

AddHandler Me.WebBrowserExcel.Navigate, AddressOf Me.OnWebBrowserExcelNavigated

或者看看这个例子:

Public Class RoutedEventAddRemoveHandler

    Private Sub MakeButton(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim b2 As Button = New Button()
        b2.Content = "New Button"
        AddHandler b2.Click, AddressOf Onb2Click
        root.Children.Insert(root.Children.Count, b2)
        DockPanel.SetDock(b2, Dock.Top)
        text1.Text = "Now click the second button..."
        b1.IsEnabled = False
    End Sub

    Private Sub Onb2Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        text1.Text = "New Button (b2) Was Clicked!!"
    End Sub
End Class