Introduction to Windows Programming for MS-DOS Programmer Summary: This article discusses some differences between programming in the MS-DOS environment and programming in the event-driven Windows environment. More Information: For the purposes of this discussion, consider the use of the term "traditional programmer" as someone who has not programmed in Windows, but has experience programming in an MS-DOS environment. As a traditional programmer, you may not only have become comfortable with a particular programming style, but also with certain accepted fundamentals, such as writing an instruction and expecting it to be carried out in a controlled order. Access Basic makes good use of Windows, making it easy to learn to program. "One Entry, One Exit" vs. Event-Driven Programming -------------------------------------------------- Consider the following pseudocode of a program designed to get user input, count all the records in a table, and display the result in a box if the user presses 1, or exit if the user presses 2. START PROGRAM LOOP WHILE TRUE GET KEYPRESS INTO X IF X IS "1" COUNT ALL RECORDS IN THE TABLE INTO Y DRAW BOX FROM ROW 10 COLUMN 5 TO ROW 12 COLUMN 7 DISPLAY Y AT ROW 11 COLUMN 6 IF X IS "2" EXIT LOOP END LOOP STOP PROGRAM The purpose of the above program is to continuously loop until a key press of a 1 or 2 is detected. At that point, a decision is made to perform some sort of operation, or to ignore the keypress and continue looping. The programmer has full control over what will happening. The Windows programming model is event-driven and graphic object oriented. In other words, programming in Windows involves creating objects and modifying aspects (or properties) of those objects based on different events. Consider the following sample program that presents two buttons to the user. If the user chooses the Count button, the program counts the records in the database and displays the result in a window. The user can press the Exit button to exit from the program. First, you create the necessary objects. Most of this phase of Access Basic programming is created graphically with the Access Forms designer. The list of controls and properties below defines a form that will be used to illustrate this. Form: "MasterForm" ------------------ Push Button: "CountButton" Caption: "Count" OnPush: "=DisplayCount()" Push Button: "ExitButton" Caption: "Exit" Caption: "=CloseProgram()" Text Box: "DisplayWindow" Note: OnPush is a property of command buttons that gives you the ability to invoke an Access Basic procedure or macro. You can then proceed to create the modules that the objects will invoke. In this case, buttons are the only objects that will have the ability to invoke procedures. The procedures shown below are pseudo-code examples. The first procedure defined is the DisplayCount procedure: PROCEDURE DisplayCount() COUNT ALL THE RECORDS IN THE TABLE INTO Y CHANGE THE DISPLAYWINDOW TEXT PROPERTY TO Y END PROCEDURE Notice that the code did not direct the resulting count to display in a box painted on the screen. Instead, the Text property of DisplayWindow was changed to the resulting count value. The next procedure defined is the CloseProgram procedure. PROCEDURE CloseProgram CLOSE MASTERFORM END PROCEDURE Notice that the procedure above does not provide an exit from some kind of loop or other program structure. Instead, it closes the object that contains the buttons and window. At this point, you have a master form object containing two buttons, a window, and a couple of coded procedures. They are in no special order, they simply exist as part of the form. So, where is the loop that checks for button activity? Where is the command to invoke the program? The answer is that these do not exist as you might expect them to. You "run" the program by opening MasterForm. When you open the form, all the control objects (that is, the buttons and so on) exist on the form waiting for something to happen. In this example, there is no flow of control (no looping to check activity). While the form is active, Windows constantly checks for events. When an event occurs, the users input is put in a queue and "waits in line" until it can be processed. For example, when you push the "Count" button, Windows detects that the button object you placed on the form has been affected. Windows sends a "Mouse Click" message to Access. Access then translates the message and determines that the DisplayCount() function should be called based on the "On Push" field of the command button. Advantages ---------- The traditional programmer will find this new approach to programming a bit challenging. There are a few things to learn and "unlearn," but there are many advantages. Windows Interface ----------------- The Windows interface is one that has been regarded throughout the industry as being very user-friendly. Familiar objects such as push buttons, radio buttons, list boxes, and a wide variety of colors and screen fonts are generally more appealing than standard ASCII text characters. The Windows Standard -------------------- Because Access Basic forces you to some extent into the Windows standard, others who are familiar with Windows applications can immediately recognize the "look and feel" of your application. This reduces the learning time because the user does not have to learn entirely new interface controls and prompts. Advantages Offered by the Windows Environment --------------------------------------------- You do not have to worry too much about different devices such as monitors, printer drivers, and so on. The Windows operating environment takes care of most device compatibility and user preference issues. In addition, because Windows handles and processes events, you will find it much easier to create and manage many aspects of an application.