ComponentPro UltimateZip

      Using events

      Language Filter: AllSend comments on this topic to ComponentPro

      The Zip class allows easy control file operations using events. This topic illustrates how to "wire-up" an event using Visual Studio environment and "wire-up" an event by yourself (if you are using the ZIP component as a class instead of adding it to the project as a component).

      Wiring-up an event using Visual Studio environment

      For C#

      1. Create a new C# project and add ComponentPro.Zip component to your form. See Creating a WinForms application for information of how to do this.
      2. Click the component icon which was created on the panel underneath the form. Right click on it and click on the Properties... menu.
      3. You should be represented with the Properties Window, click on the 'lighting bolt' icon to display all events for the ZIP component.
      4. Double-click the event you wish to "wire-up".
        win
      5. After doing this, the Visual Studio .NET environment will automatically create the necessary code to "wire-up" the event. The code should look similar to the following (using the Progress event as an example).
        void zip_Progress(object sender, ComponentPro.IO.FileSystemProgressEventArgs e)
        {
            // Show progress info. 
            Console.WriteLine("Current File: %{0} completed", e.Percentage);
            Console.WriteLine("Total: %{0} completed", e.TotalPercentage);
        
            switch (e.State)
            {
                case TransferState.StartDownloadingFile:
                    if (e.SourcePath.EndsWith(".exe"))
                    {
                        // Skip all .exe files 
                        e.Skip = true;
                    }
                    else if (e.SourcePath.StartsWith("/MyFolder"))
                    {
                        // Change the source file path if it starts with "/MyFolder" 
                        e.SourcePath = e.SourcePath.Replace("/MyFolder", "/MySecondFolder");
                    }
                    break;
            }
        }
        
      6. Now you can create a new ZIP file, add files to the archive and show progress info in the Progress event handler.

      For VB.NET

      1. Create a new VB.NET project and add ComponentPro.Zip component to your form. See Creating a WinForms application for information of how to do this.
      2. Navigate to View then select Code to switch to code view.
      3. From the "drop-down" boxes at the top of the Code Window, select the instance of the ZIP component from the box on the left, then the name of the event you would like to "wire-up" from the box on the right.
      4. After doing this, the Visual Studio .NET environment will automatically create the necessary code to "wire-up" the event. The code should look similar to the following (using the Progress event as an example).
        vb
        Private Sub zip_Progress(ByVal sender As Object, ByVal e As ComponentPro.IO.FileSystemProgressEventArgs)
            ' Show progress info. 
            Console.WriteLine("Current File: %{0} completed", e.Percentage)
            Console.WriteLine("Total: %{0} completed", e.TotalPercentage)
        
            Select Case e.State
                Case TransferState.StartDownloadingFile
                    If e.SourcePath.EndsWith(".exe") Then 
                        ' Skip all .exe files 
                        e.Skip = True 
                    ElseIf e.SourcePath.StartsWith("/MyFolder") Then 
                        ' Change the source file path if it starts with "/MyFolder" 
                        e.SourcePath = e.SourcePath.Replace("/MyFolder", "/MySecondFolder")
                    End If 
            End Select 
        End Sub
        
      5. Now you can create a new ZIP file, add files to the archive and show progress info in the Progress event handler.

      Wiring-up an event as a class

      For C#

      1. Create a new C# project and add UltimateZip component to your form. See Creating a WinForms application for information of how to do this.
      2. Navigate to the View menu then select Code to switch to code view.
      3. Create an instance of the ZIP component such as:
        // Create a new instance.
        Zip zip = new Zip();
        
      4. Now you must create a method that implements the appropriate delegate. The first step for doing this is to determine which event you would like to "wire-up". For the purposes of this tutorial, the Zip.Progress event will be used, although other events would work similarly. Registering an event code should look similar to the following:
        zip.Progress += zip_Progress;
        
      5. Navigate to the reference listing for the Progress delegate. This listing will tell you what your event-handler method should look like. According to the EventHandler<FileSystemProgressEventArgs> delegate, your method must accept two arguments (an object and an FileSystemProgressEventArgs) and return void, so create a method that does this. This method should look similar to the following:
        void zip_Progress(object sender, ComponentPro.IO.FileSystemProgressEventArgs e)
        {
            // Show progress info. 
            Console.WriteLine("Current File: %{0} completed", e.Percentage);
            Console.WriteLine("Total: %{0} completed", e.TotalPercentage);
        
            switch (e.State)
            {
                case TransferState.StartDownloadingFile:
                    if (e.SourcePath.EndsWith(".exe"))
                    {
                        // Skip all .exe files 
                        e.Skip = true;
                    }
                    else if (e.SourcePath.StartsWith("/MyFolder"))
                    {
                        // Change the source file path if it starts with "/MyFolder" 
                        e.SourcePath = e.SourcePath.Replace("/MyFolder", "/MySecondFolder");
                    }
                    break;
            }
        }
        
      6. Now you can create a new ZIP file, add files to the archive and show progress info in the Progress event handler.

      For VB.NET

      1. Create a new VB.NET project and add UltimateZip component to your form. See Creating a WinForms application for information of how to do this.
      2. Navigate to the View menu then select Code to switch to code view.
      3. Create an instance of the ZIP component such as:
        ' Create a new instance. 
        Dim zip As New Zip()
        
      4. Now you must create a method that implements the appropriate delegate. The first step for doing this is to determine which event you would like to "wire-up". For the purposes of this tutorial, the Zip.Progress event will be used, although other events would work similarly. Registering an event code should look similar to the following:
        AddHandler zip.Progress, AddressOf zip_Progress
        
      5. Navigate to the reference listing for the Progress delegate. This listing will tell you what your event-handler method should look like. According to the EventHandler<FileSystemProgressEventArgs> delegate, your method must accept two arguments (an object and an FileSystemProgressEventArgs) and return void, so create a method that does this. This method should look similar to the following:
        Private Sub zip_Progress(ByVal sender As Object, ByVal e As ComponentPro.IO.FileSystemProgressEventArgs)
            ' Show progress info. 
            Console.WriteLine("Current File: %{0} completed", e.Percentage)
            Console.WriteLine("Total: %{0} completed", e.TotalPercentage)
        
            Select Case e.State
                Case TransferState.StartDownloadingFile
                    If e.SourcePath.EndsWith(".exe") Then 
                        ' Skip all .exe files 
                        e.Skip = True 
                    ElseIf e.SourcePath.StartsWith("/MyFolder") Then 
                        ' Change the source file path if it starts with "/MyFolder" 
                        e.SourcePath = e.SourcePath.Replace("/MyFolder", "/MySecondFolder")
                    End If 
            End Select 
        End Sub
        
      6. Now you can create a new ZIP file, add files to the archive and show progress info in the Progress event handler.