Andrew H
Andrew H

Reputation: 463

Convert event delegate from c# to vb.net, calling the delegate instead of raising the event

I have scoured the web to find a solution for this, but I am left scratching my head. Here is the code, in c# I am trying to convert to vb.net. Here's the code:

    internal event EventHandler ShellItemUpdated;

    #region internal interface
    protected virtual TreeNodePath CreateTreeNode(System.Windows.Forms.TreeNodeCollection parentCollection, TreeNodePath parentNode, Tppon.Win32.ShellItem shellItem)
    {
        if (shellItem == null) throw new ArgumentNullException("shellItem");
        //
        TreeNodePath node = CreateTreeNode(parentCollection, parentNode, shellItem.Text, shellItem.Path, !shellItem.IsFolder, shellItem.HasSubfolder, !shellItem.IsFileSystem);
        node.ImageIndex = shellItem.ImageIndex;
        node.SelectedImageIndex = shellItem.SelectedImageIndex;
        node.Tag = shellItem;
        //
        shellItem.ShellItemUpdated += delegate(object sender, EventArgs e)
        {
            node.Text = shellItem.Text;
            node.ImageIndex = shellItem.ImageIndex;
            node.SelectedImageIndex = shellItem.SelectedImageIndex;                
        };
        return node;
    }

When I have tried to convert it manually, I keep getting errors about raising the event. So I used one of the converter tools on the web, and it gave me this:

    Friend Event ShellItemUpdated As EventHandler

    Protected Overridable Function CreateTreeNode(parentCollection As System.Windows.Forms.TreeNodeCollection, parentNode As TreeNodePath, shellItem As Tppon.Win32.ShellItem) As TreeNodePath
    If shellItem Is Nothing Then
      Throw New ArgumentNullException("shellItem")
    End If
    '
    Dim node As TreeNodePath = CreateTreeNode(parentCollection, parentNode, shellItem.Text, shellItem.Path, Not shellItem.IsFolder, shellItem.HasSubfolder, _
    Not shellItem.IsFileSystem)
    node.ImageIndex = shellItem.ImageIndex
    node.SelectedImageIndex = shellItem.SelectedImageIndex
    node.Tag = shellItem
    '
    shellItem.ShellItemUpdated += Sub(sender As Object, e As EventArgs) 
    node.Text = shellItem.Text
    node.ImageIndex = shellItem.ImageIndex
    node.SelectedImageIndex = shellItem.SelectedImageIndex

  End Sub
  Return node
End Function

Which is obviously incorrect, because I am not raising an event here. I've never used delegates in vb, so this is a black hole for me.

It specifically complains about this line:

shellItem.ShellItemUpdated += Sub(sender As Object, e As EventArgs) 

And says that I need to raise an event. But I want to call the delegate, not raise an event! And it appears that the whole function is gibberish. Many thanks to some genius out there help me convert this code snippet correctly...

Upvotes: 0

Views: 265

Answers (1)

dotnetom
dotnetom

Reputation: 24901

In your C# code you are not calling a delegate, you are attaching event handler to ShellItemUpdated event.

VB equivalent of adding an event handler is AddHandler. Assuming you want the same functionality like in C# code, this code should be working:

AddHandler shellItem.ShellItemUpdated, Sub(sender As Object, e As EventArgs) 
    node.Text = shellItem.Text
    node.ImageIndex = shellItem.ImageIndex
    node.SelectedImageIndex = shellItem.SelectedImageIndex
End Sub

Upvotes: 2

Related Questions