Microsoft Visual Basic .NET  
 

Although it is possible to reference the SocketTools ActiveX controls in a Visual Basic.NET project, this is not recommended. The SocketTools .NET Edition includes assemblies which are designed specifically for .NET languages like Visual Basic, and those .NET components should be used whenever possible. There are a number of significant limitations and performance penalties which are introduced when using ActiveX with modern .NET applications.

The easiest way to create an instance of the control is to add it to your toolbox and place it on a form. Simply select the Toolbox, and right-click on it to bring up a context menu, and then select Add/Remove Items. This will display the Customize Toolbox dialog. Select the COM Components tab, and scroll down to the control or controls that you wish to add, check them and click Ok. Once the components have been added to your toolbox, you can drag and drop them on your form, which will add them to your project.

Property Names

In some cases, Visual Basic.NET will rename certain properties to avoid naming conflicts within the component class. The properties function in the same way, but the prefix 'Ctl' is added to the name. The two most common properties that this is done for are the Handle and State properties, renamed to CtlHandle and CtlState respectively. For the File Transfer Protocol control, the System property is also renamed to CtlSystem.

Event Handlers

SocketTools uses events to notify the application when some change in status occurs, such as when data is available to be read or a command has been executed. To create an event handler, it is the same as how you would do it in earlier versions of Visual Basic: select the control from the drop-down listbox on the left side of the code window, and then select the event in the listbox on the right side. A new event handler will be added to your code. For example, if you choose the OnCommand event then code like this will be added:

Private Sub AxFtpClient1_OnCommand( _
    ByVal sender As Object, _
    ByVal e As AxFtpClientCtl._IFtpClientEvents_OnCommandEvent _
    ) Handles AxFtpClient1.OnCommand

End Sub

If you are familiar with Visual Basic 6.0 but new to Visual Basic .NET, the first thing that you'll notice is that the arguments are not passed individually to the event. Instead, they are passed in the argument 'e', which is a class that encapsulates all of the event arguments. The technical reference shows that there are two arguments for the OnCommand event, ResultCode and ResultString. To access their values, you would write code like this:

Private Sub AxFtpClient1_OnCommand( _
    ByVal sender As Object, _
    ByVal e As AxFtpClientCtl._IFtpClientEvents_OnCommandEvent _
    ) Handles AxFtpClient1.OnCommand

    Dim nResult As Integer = e.resultCode
    Dim strResult As String = e.resultString

    Console.WriteLine("{0}: {1}", nResult, strResult)
End Sub

Note that the arguments passed to the event handler are variants, just as they are with methods. In this case, the numeric result code and result string are written out to the console for each command that is executed on the server.

Exception Handlers

When setting a property, it is possible that the control will generate an exception as a result of an error. If these exceptions are not handled in your application, it will cause the program to halt and display a dialog box. To handle an exception, use the Try..Catch statement, such as:

Try 
    axFtpClient1.HostAddress = "abcd"
Catch comError As System.Runtime.InteropServices.COMException
    Console.WriteLine(comError.Message)
    Exit Sub
End Try

In this example, the HostAddress property is being set to an illegal value (the HostAddress property only accepts IP addresses in dot notation, such as "192.168.0.1"). As a result, an exception is thrown and the Catch section of code is executed. The ex argument contains information about the exception that has occurred, including an error number and a description of the error. In this case, that description is written to the console and the subroutine is exited.

In addition to exceptions generated when properties are set to invalid values, it is also possible to have methods generate exceptions instead of returning error codes. To do this, set the ThrowError property to True. It allows you to write code like this:

Try
    axFtpClient1.ThrowError = True
    axFtpClient1.Connect()
    axFtpClient1.ChangeDirectory(strDirName)
    axFtpClient1.ThrowError = False
Catch comError As System.Runtime.InteropServices.COMException
    axFtpClient1.ThrowError = False
    Console.WriteLine("Error {0}: {1}", Hex(comError.ErrorCode), comError.Message)
    If axFtpClient1.IsConnected Then axFtpClient1.Disconnect()
    Exit Sub
End Try

In this example, the ThrowError property is set to True, and then the Connect and ChangeDirectory methods are called. If either of these methods fail, an exception will be thrown and the code in the Catch section will be executed. In this case, the error code and description is written to the console, the client is disconnected and the subroutine returns. By setting the ThrowError property and writing your code to use exception handling, it enables you to easily group function calls together and handle any potential errors, rather than individually checking the return value for each method.