Question

I have written a DLL in C#, and I would like to get callback from an event.

In C# I dit it the following way:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using SevenZipControl;

namespace Utlility
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Zipper.ProgressEvent += ProgressChanged;
        }

        delegate void ProgressChangedCallback(int value);

        private void ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            SetProgressBarValue(e.ProgressPercentage);
        }

        private void SetProgressBarValue(int progress)
        {
            if (this.progressBar1.InvokeRequired)
            {
                ProgressChangedCallback d = new ProgressChangedCallback(SetProgressBarValue);
                this.Invoke(d, new object[] { progress });
                return;
            }
            progressBar1.Value = progress;
        }

I tried the same in VB.NET, but the IDE already complains about my AddHandler approach:

Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.IO
Imports SevenZipControl

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AddHandler Zipper.ProgressEvent, AddressOf ProgressChanged(Zipper, ProgressChangedEventArgs)
    End Sub

    Private Sub ProgressChanged(sender As Object, e As ProgressChangedEventArgs)
        SetProgressBarValue(e.ProgressPercentage)
    End Sub
    Private Sub SetProgressBarValue(progress As Integer)
        ProgressBar1.Value = progress
    End Sub
End Class

The C# implementation works fine, but in VB.NET, the IDE tells me that "Zipper" and "ProgressChangedEventArgs" are a type and cannot be used as an expression.

Can somebody tell me what I am doing wrong here? Also, it tells me that

Was it helpful?

Solution 2

The solution was much more difficult. I had to create an impromptou function:

    Zipper.Compress(uPathIn, uPathOut, Function(l1, l2)
                                           SetProgressBarValue(0)
                                       End Function)

I am not sure if that is the cleanest solution, and the compiler warns me that the function Function (l1, l2) may not have a valid return value, but it does everything I wanted.

OTHER TIPS

You have too much information in the AddHandler line. Just do:

AddHandler Zipper.ProgressEvent, AddressOf ProgressChanged
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top