Learn the most popular programming language of the most popular Operating System: Windows

Ad

Compiled Application from Standard EXE Project

This final objective asks you create an executable file from a completed Visual Basic application.  Two options are available: Under the Compile tab of the Project Properties dialog box, you can select Compile to P-Code or Compile to Native Code (see Figure 2.10).  Compiling to native code is the default.

Figure 2.10The Compile tab of the Project Properties dialog box enables you to control the way in which you compile your completed application.

Interpreted P-Code

By selecting P-Code, the modules of your Visual Basic application are compiled into what is called pseudocode or false code.  An interpreter program that then produces machine language instructions reads the coded instructions.  All versions of Visual Basic prior to Visual Basic 5.0 were interpreted from P-Code.

Interpreted programs offer some advantage.  Applications can be designed rapidly because new code can be tested immediately, without time-consuming compilation.  They also offer some security advantages in networked applications.  You can rely on the interpreter to serve as a buffer between an application and computer hardware.  This is the security model built into the Java programming language, for example.  To their disadvantage, interpreted programs tend to run slower than compiled programs.

Compiled Native Code

With Visual Basic 5.0, you can create native code compiled applications:  The compiler takes the modules of your project and creates a machine language compiled application.  Although the native code executable does not require interpretation, it still depends on other files to run.  These files consist, mainly, of the object linking and embedding (OLE) libraries that make up the ActiveX architecture upon which Visual Basic is based.

One way to think about the difference between interpreted and compiled programs is to compare it to translating a letter from English to German (or another language).  A compiled program translates the entire letter once, producing machine-language instructions.  An interpreted program translates each letter every time the letter is read, producing P-Code, which is then translated into machine language.

The Make Tab of the Project Properties Dialog Box

When you decide to create an executable, the Make tab of the Project Properties dialog box comes into play.  The Make tab presents general attribute options for your executable (see Figure 2.11), including the title of the application, the icon used to represent the application when it is minimized, and different version options. The Make tab also enables you to set command-line arguments for your application should you decide to use them.

Figure 2.11The Make tab enables you to set version numbers, version information, application attributes, command-line arguments, and conditional compilation arguments.

Exercise 2.9  Creating an Executable Application

This exercise asks you to create a make file from the Happy Birthday application created in Exercise 2.8.

  1. Open Exercise 2.8 if it is not on the screen.
  2. Choose File, Make Project2-8, if this reflects the name you gave your project.
  3. When the Make Project dialog box opens, select a file name, such as BDAY1.EXE, and click OK.  That's it. You have created a native code executable file. Remember that native code is the default.
  4. Choose File, Make Project2-8 again, and when the dialog box opens, click Options.
  5. Click the Compile tab.
  6. Click Compile to P-Code, and click OK to return to the Make Project box.
  7. Select a different file name, such as DAY2.EXE.
  8. Close Visual Basic, and start Windows Explorer.  Locate both DBAY1.EXE and BDAY2.EXE.  Click BDAY1.EXE to run the program.  Close BDAY1.
  9. Click BDAY2.EXE to run the program.  Close BDAY2.
Share:

Writing Visual Basic Instructions-2

Exercise 2.6  Changing the Font Size Property

In this and the next two exercises, you change the properties of the form used in Exercise 2.5.

  1. Choose File, Open Project, if necessary, and select View, Project Explorer, or click the Project Explorer icon to open the Project window.  Click View Form.
  2. Click the form to give it the focus, and press F4 to open the Properties dialog box.
  3. Click font and change the font size to 18 points (or to another size if the size is not available on your system).
  4. Run the program.  Click to display the first sentence.  Press a key to display the second sentence.
  5. Exit to continue with the next exercise.

Exercise 2.7  Changing the Font Style Properties

In this exercise, use the form from Exercise 2.5 once again.

  1. Open the Properties dialog box and open the Font dialog box by double clicking the Font property.  Change the Font to Bookman Old Style (or another font if your system does not have this font installed).
  2. Change the style to bold and italic.
  3. Run the revised program.  Click to display the first sentence (it should be in bold and italic).  Press a key to display the second sentence.
  4. Exit and save your project at this time.  The next exercise asks you to begin a new project.

Exercise 2.8  Adding a Message to a Form and Changing Property Settings

In this exercise, you write a new procedure and change properties.

  1. Open a new project.
  2. Add the following Form_Click() Visual Basic procedure to the form:
    Private Sub Form_Click()
    'This procedure will display a Happy Birthday message
    Print "                 HAPPY"
    Print "                 BIRTHDAY"
    End Sub
    
  3. Change the following form properties:
    PropertySetting
    Backcolor Palette= &H00FF00FF& (light purple)
    BorderStyle= 1 'Fixed Single
    Caption= Today is the day
    Font Style= Italic
    Font= Arial
    Font Size= 24
    ForeColor Palette= &H00FF0000& (blue)
    WindowState= 2 'Maximized
  4. Run your program.
  5. Exit and save your program. Give the form a unique name, such as BDAY.FRM. Give the project a similar name, such as BDAY.VBP.
Share:

Writing Visual Basic Instructions

The final type of window discussed here is the Code window.  To open the Code window for a form, click the Project Explorer and click View Code, or choose View, Code.  This opens the window shown in Figure 2.7.  The object shown is the Form; the procedure is shown as Load().  The title bar reads Form1 because you have not changed the name of the form.

Figure 2.7

This Code window shows the start of a Form_Load() procedure.

The Code window contains the first two coded instructions, which are entered for you.  The first combines the name of the object with the name of the event procedure.  This coded instruction is shown in the following example, in which Load() is a special type of event:

Private Sub Form_Load()

It occurs when a form is first loaded.  However, suppose you want to change the event from Load() to Click().  By selecting Click (click the procedure list box and scroll to Click, as shown in Figure 2.8), the first coded instruction changes to the following, in which Click() is another type of event:

Private Sub Form_click()

It is triggered by a mouse click.

Figure 2.8

The Code window now shows the start of a Form_click() procedure.

The event has now been changed from occurring at the time the form is loaded to the time when the form is clicked, with the user controlling the time of the event with the click of the mouse.

The ending instruction stays the same.  It reads as follows, which means the end of the procedure (sub):

End Sub

Exercise 2.4  Adding a Click() Procedure

In this exercise, you begin a new project.

  1. Choose File, New Project and open a Standard EXE project.  If the last project is still on-screen and has not been saved, you will be asked if you want to save changes to P2-3.vbp? Click No, unless you have already saved it.  Either way will not lead to problems.
  2. When Form1 appears, double-click to open the Code window.
  3. Change the Project name to Project2_4.
  4. Change the procedure name from Form() to Click() in the procedure list box.

    Your code should read as follows:

    Private Sub Form_Click()
    End Sub
    
  5. Type the following to create the procedure:
    Private Sub Form_Click()
    'This procedure will display a message on the form
    Print
    Print
    Print "When you click, look at what happens!"
    End Sub
  6. Close the Code window by double-clicking the Control box icon.
  7. Choose Run,Start;click the Start icon on the toolbar; or press F5 to run your procedure. When Form1 appears, place the mouse pointer on the form and click. What happens?
  8. Click the form again. What happens? It makes you want to click forever doesn't it?
  9. Choose Run, End from the menu bar or press the End button on the toolbar to return to the design of Form1.
  10. Save this form and project. You can call the Form F2-4.frm and the project P2-4.vbp if you want to use them in later exercises; however this is not necessary. Continue with the next exercise using the same form.

Exercise 2.5 Adding a KeyPress() Procedure

In this exercise you add a second procedure to a form. Figure 2.9 shows the revised form. This exercise assumes you have completed Exercise 2.4 and are using that form.

Figure 2.9

Adding a KeyPress() procedure to the form places more than one procedure on the same form.

  1. Choose View, Code from the menu bar. The code you wrote for Exercise 2.4 should appear.
  2. Choose KeyPress from the Procedure list box, and observe what happens to your code.
  3. type the following to complete this procedure:
    Private Sub Form_KeyPress (KeyAscii as Integer)
    'This procedure will display a different message on the screen
    Print
    Print
    Print "    When you press a key, look at what happens!"
    End Sub
  4. Close this procedure (using the Control menu icon) and run the revised program. There are four ways to run your program:
    • Choose Run, Start.
    • Choose Run, Start with Full Compile.
    • Press F5.
    • Press the Start button on the toolbar.

    This time, press F5 to start your program; then as a test, do the following:

    1. Click the form.
    2. Press a key.
    3. Press a second key.
  5. Click the End button on the toolbar to stop your program a different way, or choose Run, End to stop the program.
  6. When you are returned to Form1, double-click the form to open the Code window. What code appears?
  7. Scroll through the list of procedures in the Procedure menu. Coded procedures are displayed in bold.
  8. Save this form and project at this time. You will be asked to work with the form and project in the next several exercises, but it is wise to save your work periodically. You can save your form as F2-5.frm and your project as P2-5.vbp to keep your examples in sequence. Name the project Project2_5 before saving.
Share:

Disabling the Minimize Button

With the first two border styles, the Minimize button appears unless it is disabled by the Minbutton property to change the setting to False.  (With this property, there are only two choices: True and False.)

Disabling the Maximize Button

With the first two borderstyles, the Maximize button appears, unless it is disabled.  To disable the button, double-click the MaxButton property to change the setting to False. (Like MinButton, only two choices are available with MaxButton: True and False.)

Disabling the Control Box

With the first three border styles, the control box appears unless it is disabled.  To disable the box, double-click the ControlBox property to change the setting to False. (The two settings for ControlBox are True and False.)

Changing the Window State

The WindowState property determines the appearance of the form at runtime.  Table 2.3 shows its three settings.

Table 2.3 The Settings for the WindowState Property
ValueSettingDescription
0 Normal The form appears in normal size (the default).
1 Minimized The form is minimized and appears as an icon (either on the taskbar or above it) when the application is run.
2 Maximized The form is maximized and expands to fit the entire screen when the application is run.

Changing the background Color

Part of the enjoyment of working with Visual Basic is the ease with which colors can be added to a form.  Color settings are expressed in hexadecimal notation (base 16). The designers of Visual Basic did not expect you to work with hexadecimal numbers and instead provided a color palette.  To open the palette, double-click the Backcolor setting and click Palette.  When the Color palette appears, click a color to change the background color of the form.  Following the click, the hexadecimal number assigned to the color appears in the text box.

Changing Font Properties

When working with text, Visual Basic enables you to change the font (or text style) you are working with.  To determine which fonts are available on your system, open the Properties window, highlight Font, and click the ellipsis button to the right.  A dialog box appears for setting numerous font properties, including Name, Bold, Italic, Size, StrikeThrough, and Underline.

Changing the Foreground Color

The ForeColor property determines the color of the text and graphics to be displayed.  This differs from Backcolor, which determines the background color of the object.  To set this property, double-click ForeColor and click Palette.  Make the color selection using the palette.

Changing the Startup Position

The StartUpPosition property specifies the position of the form when it first appears.  Table 2.4 shows its four settings.

Table 2.4 The Settings of the StartUpPosition Property
ConstantValueDescription
vbStartUpManual0No initial setting specified
vbStartUpOwner1 Center on the item to which the UserForm belongs.
vbStartUpScreen 2Center on the whole screen.
vbStartUpWindows3Position in upper-left corner of screen.
Share:

Popular Posts

Search This Blog

Powered by Blogger.

Featured Post

Coded Statements and Methods

In the preceding procedure , you wrote several coded instructions, which introduced two different programming statements and a method: the D...

Recent Posts