March 20, 2019
Hot Topics:

Create a GUI for an FTP Client with VB.NET

  • October 29, 2004
  • By Paul Kimmel
  • Send Email »
  • More Articles »

Implement an FTP GUI

Having added the GetFileList, CreateDataSocket, and RequestResponse methods to your FtpClient class (see "Write an FTP Client with VB.NET to Bridge Legacy Software" for the complete listing of the FtpClient), you can now design a presentation layer around the Connect, Login, Disconnect, and GetFileList capabilities of your FtpClient class. You then add a VS.NET 2005 menu, status control, and a user control that can display the files returned from the server.

Add a MenuStrip to the Form

The next version of VS.NET replaces the MenuBar with a MenuStrip. The basic functionality is the same, but I find using the MenuStrip a bit clumsier. For example, to add a separator to menus in previous versions of VB, one only had to use the hyphen (-) as the text for the MenuItem. This was exceptionally easy. Whidbey (VS.NET 2005) objectifies as a ToolStripSeparator the indented line that separates regions of a menu. Instead of using a hyphen to create the separator, one needs to right-click over the menu in design mode and select Edit DropDownItems... from the context menu (see Figure 1).

Click here for a larger image.

Figure 1: Modifying Menus Is a Bit More Confusing in Whidbey

Selecting the Edit DropDownItems menu opens an Items Collection Editor (see Figure 2) where you can have more precise manipulation of MenuStrip elements. (Beta software is subject to change, so hopefully Microsoft will simplify the MenuStrip behavior.)

Click here for a larger image.

Figure 2: Adding a Separator Requires Use and Knowledge of the Items Collection Editor.

MenuStrip behavior—once the menu is created—is similar to MenuBar behavior. Define the StripMenuItem and double-click to generate an event handler. For this article, I added File, View, and Help StripMenuItems. File supports connecting, disconnecting, and exiting. View supports requesting a remote files list, and Help has an About sub-item. For each of these menu items, I created an event handler in the usual way and invoked the related capabilities.

File|Connect invokes the FtpClient.Connect and FtpClient.Login behaviors. File|Disconnect invokes the FtpClient.Disconnect behavior. View|Get Remote Files invokes the FtpClient.GetFileList behavior, and Help|About uses MessageBox.Show (MsgBox) behavior to display a simple About dialog box.

The Form_Load method is used to create an instance of the FtpClient object and a FileListUserControl dynamically. (Find the complete listing for the form at the end of this article.) Whidbey presently separates designer-generated code from user-generated code. It will add designer-generated code to a file named filename.Designer.vb. For example, if you have a form named Form1, Whidbey will create Form1.vb, Form1.Designer.vb, and Form1.resx. Generally, you will add your code to Form1.vb in this scenario.

Create a File List User Control

To support listing files on the client and server, you can implement one user control and ultimately use it twice: once for the client-side files and once for the server-side files. In this example, you instantiate one user control for server-side files.

The FileListUserControl is comprised of a Label, a Panel with a Button and ComboBox, and a TreeView. You use the Label to indicate what the control contains. You can ultimately use the Button and ComboBox to change folders or drives where applicable and the TreeView to display the files discovered on the client or the server. For practical purposes, you can surface the Text value of the Label to permit consumers to change it, and hide the ComboBox when the FileListControl represents the remote server and show it when the FileListUserControl represents the client. Figure 3 shows the design-time view of the FileListUserControl.

Figure 3: The FileListUserControl Is a Generic User Control That Accepts a String Array But Is Generally Unaware of the Origin or Meaning of the Data.

The FileListUserControl is straightforward: Pass an array of strings and the SetFiles method calls an Add method for each string in the array, adding the string to the TreeView control. Listing 2 contains the code for the FileListUserControl. (The designer-generated code is contained in a file named *.designer.vb in Whidbey.)

Listing 2: The Implementation of the FileListUserControl

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.IO
Imports System.Windows.Forms

Public Class FileListUserControl

    Public Property Caption() As String
            Return Label1.Text
        End Get
        Set(ByVal value As String)
            Label1.Text = value
        End Set
    End Property

    Public Sub SetFiles(ByVal files() As String)

        Dim file As String
        For Each file In files
            If (file.Trim() <> String.Empty) Then
            End If
    End Sub

    Private Sub Add(ByVal file As String)
        Broadcaster.Broadcast(String.Format("Adding {0}", file))
        treeViewFiles.Nodes.Add(New TreeNode(file))
    End Sub

    Private Sub FileListUserControl_Resize(ByVal sender _
    As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Resize
        treeViewFiles.SetBounds(0, 80, Width, Height - 80)
        Label1.SetBounds(0, 0, Width, 40)
        Panel1.SetBounds(0, 40, Width, 40)
    End Sub
End Class

Page 2 of 3

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date