VB has become an enormously successful application development language. One reason it is so successful is the ease with which a beginner can develop a Windows GUI. This article explains the visual programming strategy that Microsoft used in its VB IDE.
According to Microsoft, more than three million programmers are developing software applications using Visual Basic (VB). This makes it by far the most widely used visual development tool on the planet. The appeal of VB is, in large part, because of its ability to balance ease of use with robust functionality. This balance is evident when looking at the VB Integrated Development Environment (IDE). With each successive version, Microsoft has enhanced the IDE to provide a more powerful programming environment. With Visual Basic Version 5.0, the programmer has powerful development tools tightly integrated into an IDE that is customizable to each individual’s preferences. This article demonstrates how to use the VB IDE to develop a visual software application.
When you start VB, it displays a dialog box that asks you which type of application you want to create. While a number of choices are available, you essentially choose whether your project is to be a standalone application or a component to be used by other applications. Figure 1 shows the VB New Project dialog box.
As the AS/400 provides tools such as SEU and SDA to assist in software development, the IDE is used to create VB applications. Just as its name implies, the IDE provides a single integrated environment for screen design, code editing, project compilation, online help text, and interactive debugging. Figure 2 shows the VB IDE.
The VB IDE consists of six main components: the Toolbox, the Form Design Window, the Code Window, the Project Window, the Properties Window, and the Form Layout Window. Each window performs a specific task to assist you with the development of a VB project.
The VB Toolbox contains a variety of controls that may be added to your project. When added to a VB form, controls such as text boxes, list boxes, labels, and command buttons provide the user interface for your software application. Controls are prepackaged objects that provide specific functionality to the applications you construct using VB. The characteristics and functionality of a control are accessed in your VB programs via properties, methods, and events.
Properties are the attributes that commonly define the appearance and/or characteristics of a control. The attributes of a control are changed by manipulating the values of the control’s properties. For example, to change the color of a screen heading’s text, you simply change the value of the Label control’s ForeColor property to the desired color.
In addition to properties, most controls have methods that perform predetermined functions. A method can be thought of as the functionality that is built into a control. To trigger the control to do something, you call one of the control’s predefined methods.
Unlike methods, event procedures allow you to associate your source code with a predefined control action. The code you add to an event procedure automatically executes when the event occurs. Some examples of common control events are clicking on a command button, keying text into a text box, and displaying a form. Event procedures allow you to say “when the user clicks this button, execute this VB source code.” VB comes with a set of standard controls that are adequate to build most applications, each possessing unique properties, methods, and events.
The Form Design Window
The Form Design Window provides a workspace that allows you to design the layout of your application’s forms. Conceptually, the Form Design Window does for a VB programmer what SDA does for an AS/400 programmer.
The Code Window
The Code Window provides a workspace that allows you to edit and organize VB code statements. Conceptually, the Code Window does for a VB programmer what SEU does for an AS/400 programmer.
The Project Window
The Project Window provides an organized view of your project’s components. From the Project Window, you can see all of the forms and code modules that make up your application.
The Properties Window
The Properties Window displays the attributes or properties of the selected component. Properties can be thought of simply as the attributes that define a component’s appearance and behavior. For example, if you are designing a form in the Form Design Window, the Property Window displays the form’s properties. If you select a specific control on the form, such as a button, the properties associated with the button are displayed.
The Form Layout Window
The Form Layout Window is used to define the positioning of a form on the screen. When the application runs, the form is displayed according to the positioning defined in the Form Layout Window.
The AS/400 Message Utility
A sample application called the AS/400 Message Utility demonstrates how the IDE is used to develop an application. The application is a simple client/server utility that is used
to send a message to one or more active AS/400 users. Figure 3 shows the completed AS/400 Message Utility.
To create applications such as the AS/400 Message Utility, you will start VB and open a new project defined as a standard executable (EXE), as shown in Figure 1. The VB IDE presents you with a new project given a default name of Project1 and a blank form given a default name of Form1. To rename the project, you will click on the File menu, then on the option labeled Save File As. This will allow you to give your project a suitable name, such as “Message”.
From the IDE, you will use the Form Design Window and Toolbox to create the application’s screen or form. Once you have decided how you want the form to look, simply select the desired control on the Toolbox and draw the control onto your form. To draw the control onto the form, you click your left mouse button on the desired location on the form. While holding down the button, simply drag the mouse across the form until the control appears in the desired dimensions. Once a control has been added, you may hone the look of your application by moving or resizing it using your mouse. Five basic controls are used in the sample application:
1) A VB frame allows you to provide a logical grouping of controls on your form. It appears as a lined box drawn around areas of the form. The sample application uses two frames to separate the list of users from the message entry, as shown in Figure 4.
2) The sample application employs a list box control to contain a list of active AS/400 users. Much like an AS/400 subfile, the list box provides an automatic scrolling mechanism that allows you to organize a large list of entries. Properties are set for the list box using the Properties Window, which allows a user to select multiple entries, as shown in Figure 5.
3) Text boxes are used to provide an input-capable field on your form. A text box is used in the sample application to accept a message to be keyed in by the user. The MaxLength property is set to 512, limiting the length of input to be accepted, as shown in Figure 6.
4) Label controls are typically used to define constant text, such as field headings on a form. In the sample application, a label control is used at the bottom of the form to provide a status message area. Properties are set on the control to give the label the appearance of a three-dimensional box, as shown in Figure 7.
5) The Command Button control provides a push button mechanism for the user interface. The sample application uses command buttons to refresh the user list and to send the message keyed into the text box control, as shown in Figure 8.
Using Third-party Controls
As previously stated, VB comes with a set of basic controls suitable for most visual applications. However, these basic controls sometimes don’t provide the functionality or the look you want for a particular application. One of the greatest strengths of VB is that it allows you to use controls created by outside sources. The third-party control market is huge, with hundreds of companies selling VB add-in components. For instance, you may want to create an application that accepts input of a date. Rather than having the user key in a date, you may want the user to see a calendar and click on the desired date. Probably a dozen or more companies sell calendar controls that can be added to your VB applications.
Even IBM provides a set of three controls as part of Client Access for Windows 95/NT. The IBM controls provide access to AS/400 functions from within your VB application. Since these controls were designed to conform to Microsoft’s ActiveX standard, they can be used by any ActiveX-capable software. This means that in addition to
using the controls in VB, you may also use them in packages like Excel, Access, and Word. The Message Utility application uses all three available Client Access controls to provide client/server connectivity to the AS/400.
System List Box
The Client Access System List Box control returns a list of attached or configured AS/400 system names. The control automatically interrogates your Client Access connection to determine the name of your AS/400. The control eliminates the need to hardcode a system name in your VB program, making your applications more maintainable. When used in a multiple AS/400 environment, the control can be used to allow the user to select the desired AS/400 from a list. For single system environments, the functionality of the control can be used without making the control visible to the user. The control can be hidden from view by setting the control’s Visible property to false.
Remote Command Button
The Client Access Remote Command Button control is used to execute an AS/400 command. The sample application uses this control to call a processing program that creates a list of active system users and writes each user profile name to a data queue. When supplied a system name, as retrieved by the System List Box control, the Remote Command Button control executes a CL command on the specified AS/400. Like the system list box, the functionality of the Remote Command Button control can be used without making the control visible to the user. The control is hidden from view by setting the Visible property to false.
Data Queue Text Box
The Client Access Data Queue Text Box control gives your VB applications read and write capabilities to an AS/400 data queue object. This functionality provides a simple and effective means of transporting data to and from an AS/400. The Data Queue Text Box control handles the chore of converting data between ASCII and EBCDIC formats automatically. The functionality of the Data Queue Text Box control can be used without making the control visible to the user. The control is hidden from view by setting the Visible property to false. The sample application uses this control to read entries from a data queue for each active system user.
Adding Controls to a Project
To add third-party controls to your project, open the Project menu and select the Components option. A dialog box will display a list of components available for use by your project, as shown in Figure 9.
Selecting the Client Access Control Library will allow your project to use the three Client Access controls. Once selected, the controls will appear on your toolbox in the IDE, as shown in Figure 10.
From the toolbox, you will add the controls to the form just as you added the frame, list box, label, text box, and command button controls.
Once the visual design of the project is completed, the remaining task is to add functionality to the application in the form of source code statements. To switch the IDE from the Form Design Window to the Code Window, double-click on the control you want to code. The Code Window displays the event procedures available for the selected control. For example, if you double-click on the Command Button control, the button’s event procedures are displayed in the Code Window. The Code Window allows you to easily switch between events and controls. You will key in VB code that you want to execute when the selected event occurs. For the application command buttons, you will select the Click event and key in the code that you want to execute when the button is
clicked. To create a simple application like the Message utility, most of the code involves setting control properties and executing methods. For example, when the user clicks on the button to refresh the AS/400 user list, the button’s Click event procedure, as shown in Figure 11, executes.
To be fully functional, the Message Utility requires a fair amount of code that is beyond the scope of this article.
Simple, Yet Powerful
As you can see by this quick overview, the VB IDE provides a well-designed integrated development tool, balancing the ease of use needed for weekend hobbyists and functionality demanded by enterprise application developers. For AS/400 client/server developers, VB’s ability to use add-in components makes it an attractive option. Many third-party tools are available to make connection to an AS/400 easy.
Figure 1: The VB New Project dialog box
Figure 6: Adding a text box to the form
Figure 7: Adding a label to the form
Figure 10: The VB IDE toolbox
Private Sub cmdRefresh_Click() ‘‘ Initialize captions and buttons by setting properties ‘pnlStatusBar.Caption = “Calling AS/400 Program...” cmdRefresh.Enabled = False mnuRefresh.Enabled = False pnlStatusBar.ForeColor = vbBlue
‘ Clear list box of users using Clear method lstUsers.Clear ‘‘ Load the Call command into CommandButton and ‘ execute DoClick Method to send command to AS/400 ‘crcCommand1.CommandString = “CALL MCLIB/AUSRC”
crcCommand1.DoClick Figure 11: The Click event