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

Ad

Invisible Objects in Visual Basic

Not all objects in Visual Basic are visible and displayed to the user.  Neither are all object windows.  Visual Basic contains system objects that are available to use, although they are not displayed in the toolbox, including the following:

  • App – This object stores information about the application, including its name, path (where it is stored), and the files it requires.  This object can also activate an application.
  • clipboard – This object is a temporary storage location.  It allows text, graphics and computer code to be transferred from one part of an application to another part or from one application to another.
  • screen – This object references the entire video display screen.  It manipulates the size and placement of forms on the screen.
  • Debug – This object permits the display of information about a program while it is being developed.
  • Printer – This object determines how text and graphics are printed on a page and how output is sent to the printer.

These are system objects because the Windows operating system keeps information about each one.  For example, when you set up Windows, you must designate the computer printer to be used to send output to the printer.  When a specific printer is selected, Windows stores information about its makeup, such as the paper width and length.  In Visual Basic, you never have to use your printer’s name in a program.  Instead, you can instruct your printer to print directly by using the Printer object.

System objects will become important later.  For now, you should simply be aware that they exist and of what they do.

Technorati Tags:
Share:

Understanding Objects as Instances of Classes

In the last example the two text boxes and the form we have tried are referred to as windows as well as objects.  In a Windows program, just about everything that you see on the screen is a window, and many items that you don’t see are windows.

In a standard Windows word processing program, such as Word or WordPerfect, every button on the toolbars is a separate window.  Every open document is a separate window, as are the dialog boxes that permit you to select different font properties, and the rulers that allow you to set tabs and adjust page width.  These windows are objects, which have their own properties, methods, and events.  The Windows operating system sends messages to the windows to respond to user events.

Object Classes

How many different kinds of windows can there be? As many as a programmer can imagine and create!  The design of the windows operating system provides several standard window types called classes.  There can be many classes of objects in addition to the standard types.  Whether you know it or not, you probably are already familiar with several standard Windows classes and their behaviors, including the following:

  • Button
  • ComboBox
  • Edit
  • ListBox
  • Scrollbar

As you might suspect by now, some of the controls in the Visual Basic toolbox are related to these standard Windows classes.  For example, a Text Box control is closely related to the base Windows Edit class.

A class is like a cookie cutter: It defines how an object should look and behave when it is first created.  to work with Visual Basic objects, you first use this cookie cutter to cut out the uniform shape of your object.  After you have this basic shape (called an instance of the class), you can manipulate the object by altering its properties or by calling one of the object’s methods to make the object behave in a certain way.

Often Visual Basic creates an instance of a class for you.  When you add a Text Box control to a form, Visual Basic creates an instance of the visual Basic Text Box control class.  After it is created, you can manipulate it.  To alter text displayed in a text box objects named txtDisplay, for instance, you would execute the following instruction:

txtDisplay.Text = "Welcome to Visual Basic"

In other circumstances, you will be required to create an instance of the class (an object) yourself.  You will even define your own classes and then create multiple instances (objects) from the class you have defined.

Technorati Tags:
Share:

Visual Basic Statements and Instructions

The discussion of objects and encapsulation in "Forms and Controls", uses several lines of Visual Basic code, including examples of object properties being assigned values and methods being invoked.  The terms code and instructions can be used interchangeably, but the term statement means something specific in Visual Basic.

Visual Basic Statements

A statement differs from the assignment of a property or invocation of a method.  Consider the following definition: A statement is a reserved word that causes a computer program or the operating system to do something.  Statements are instruction (code) that are built in to the Visual Basic language.  Methods are code held by an object.

If you look at the Visual Basic Help, you discover that some keywords are statements.  for example, the keyword End is a statement that ends (stops) the execution of a Visual Basic program.  The keyword Kill (sorry, this is in the language) is a statement that deletes a file from a disk.

As another example, the statement to force explicit declaration of all variables is written as follows:

Option Explicit

In this example, no reference to an object is required because the statement is built into the language.

Visual Basic Program Instructions

Methods, statements and Visual Basic instructions are different.  An instruction is simply a line of code in a Visual Basic application.  Instructions contain the names of constants, variables, functions, properties, methods and built-in Visual Basic statements.  The rule to remember regarding instructions is that all instructions begin with a statement or the invocation of an object method.

  For example, an instruction beginning with a statement is as follows:

End

The following is an instruction beginning with a method:

List1.AddItem Entry

The method is AddItem and the object is List1.

Finally, some instructions beginning with methods or statements also contain what are known as statement or method arguments.  Consider the following:

Form1.Move Left + (width \ 10), Top + (Height \ 10)

In this instruction, Move is a method and Left, width, Top and Height are properties of the object, Form1 that are used as method arguments.  An example of a built-in Visual Basic statement that takes an argument is the AppActivate statement:

AppActivate “Calculator”

In this case, the statement shifts focus to a running instance of the Calculator utility bundled with Windows.

This all may seem a bit confusing right now, but you will get used to working with objects, methods and statements once you begin writing Visual Basic programs.

Technorati Tags: ,
Share:

A Closer Look at Encapsulation

Object properties, methods and events make up what is known as the objects interface.  this interface may be considered the private view of the object and it describes how that object can be manipulated.  Objects also have a private view: the data and code that the object does not expose to public view, but retains in order to execute its functions.  When you invoke the clear method of a List Box control, for example, you do not know how the list box clears its contents.  It just does it and you know that it works.  To illustrate encapsulation, let’s conduct a brief and simple exercise.

  1. Start-->All Programs -->Microsoft Visual Basic 5.0-->Visual Basic 5.0
  2. Select Standard EXE Project from the Project Type Window.
  3. The Project Window is displayed with a Blank Form (Form1).
  4. Select the Text Box Tool from the Toolbox and draw a Text Box on Form1.
  5. Draw Another Text Box below the first Text Box.
  6. Select Start from the Run Menu.
  7. Set the insertion point in the upper text box and type Microsoft Visual Basic.
  8. Select the text you have written using your mouse (or hold the Shift key down and use the directional keys).
  9. Copy the selected text onto the Windows Clipboard (Ctrl+C).
  10. Using the Tab key, shift to the lower text box and paste the text (Ctrl+V) from the Clipboard into the lower text box.
  11. Using the Backspace key, delete part of the text that you pasted.
  12. You can maximize the Form to fit in the Application Window or minimize it to remain as an icon on the task bar.
  13. Click on the Close Button of the Form at the right top corner to stop running the program.

Consider all the functionality contained within this application without a single line of Visual Basic code being written.  Three windows, a form, and two text boxes, were created.  One of these windows, the form, uses the default colors specified in the Windows Control Panel.  This form can be resized, minimized into an icon, or maximized to fill the entire screen.  It can be moved around the screen with the mouse.

The two text boxes allow the user to erase existing text and type text.  Text could be copied and pasted.  The text controls even indicate the user’s location by displaying a cursor.

How does the Text Box control manage the insertion point?  How does it "know" to erase text when the Backspace key is pressed?  You don't know.  That's the private view of the Text Box object.  You don't really have to know the inner workings of the Text Box object.  It works, and you can use it to build your own applications.  That's the power of encapsulation!

Technorati Tags: ,,
Share:

Object Properties

A property is a named attribute of an object.  Properties are used to change the appearance and behavior of objects, such as forms and controls.  These properties include attributes affecting the color, size, and name of the object.

Let’s take an everyday object, such as a shirt.  Suppose you write the following:

Shirt.Color = “Blue”

In Visual Basic, this would tell you that you have an object named Shirt. One of the properties of the shirt is color. You can change the color by assigning a new one, such as "Blue". You might want to set the shirt property as

Shirt.launder = “Clean”

as opposed to

Shirt.launder = “Dirty”

In the example, the condition of the shirt – clean or dirty – can be set directly.  Wouldn't it be great if your shirts were always clean?

For a Visual Basic object, such as a form, you can write instructions to alter it.  The following instruction changes the color of a form named Form1 to blue:

Form1.BackColor = vbBlue

Object Methods

The concept of an object method is more difficult than the concept of a property.  Methods are computer instructions (code) held by an object that operate on the object’s data.  Methods are written in a different way than properties.  The syntax (required way) for describing the application of a method to an object is written as follows:

object.Method

Perhaps an analogy will make methods more understandable.  Suppose you had a very compliant dog named Sparkyy.  By invoking the following methods of Sparky, you would get the behavior from Sparky that you desire:

Sparky.eat

Sparky.bark

Sparky.scat

Object methods in Visual Basic are similar.  One method for a List Box control (a control that displays a list of different text items) is the Clear method.  To clear the contents of the list box, you would write the following instruction:

List.Clear

When working with object properties in Visual Basic, you are either assigning them a new value (for example, Form1.BackColor = vbBlue), or you are reading their values (for example, mycolor = Form1.BackColor). Object methods, by contrast, are invoked with the object.Method syntax (for example, List1.Clear).  The distinctions between object methods and object Properties will become familiar to you as you work with Visual Basic objects.

Object Events

While the value of a property can be set and a method can be invoked, an object event is an action taken by the object when notified by a message.  An object event might be a mouse click.  The handling of object events is an operating system responsibility.  The Windows operating system sends messages to windows (objects) for different events.

Remember the words message and action.  If the user clicks a mouse button on a Visual Basic form, the operating system sends a message to the form.  The Visual Basic designer can choose to execute code (the action) in response to the message or ignore it.  For example, an image on a form might be programmed to take some action when it is clicked.  It might take some other action when it is dragged across the form.  The Visual Basic programmer writes coded instructions for the events that should be assigned to the form or control.  Visual Basic code, for instance, executes instructions by invoking a form’s Print method when the user clicks the form.

Private Sub Form_Click()
  Form1.Print "You clicked the form1"
End Sub
Share:

Forms and Controls

What, exactly, does the phrase "encapsulates the Windows PI" mean?  Encapsulation is a key concept for any object-oriented programming language.  It refers to the capability of an object to hide its internal workings from other objects.  In turn, the object allows itself to be manipulated by the programmer and other objects through the use of three key object features: properties, methods, and events.

Let’s consider two kinds of objects that you have seen in the Visual Basic startup screen: forms and controls.  The image below shows a Window that contains a form named Form1.  This form acts as a container for controls.  You could think of Form1 as a blank piece of paper that you must fill in.  You use this blank form to design the user interface for a Visual Basic application.  When you begin the design of a Visual Basic program, you start with a blank form and, with a set of tools, begin to design the user interface.

The tools you use to construct the interface are called controls, which are attached to forms.  Examples of controls are shown on the toolbox and include a text box (a box the user types text into), a command button (an on-screen button that the user can click), and a label (an area on the form to place a text heading).  If the toolbox is not visible, select View, Toolbox on the menu bar, or click the toolbox button on the toolbar to display it.

Because forms and controls are objects, they can be manipulated by properties, methods, and events.  Let’s consider each of these object features in turn.

Share:

Understanding the Relationship of VB to Windows

All application programs (the kind of programs developed with such languages as Visual Basic) must be developed to work with a specific operating system.  Application Programs interact with operating systems by using operating system functions that applications developers can exploit.  These functions are usually referred to as application program interface (API) functions.  API functions allow programmers to add such functionality to their programs as opening and saving files, checking system hardware, using the computer’s internal clock, and, in the Microsoft Windows family of operating systems, creating, modifying, and communicating with Windows.

The Windows API is enormously complex.  It has over 800 functions, some of which can perform a wide variety of tasks.  The value of Visual Basic is that it greatly simplifies Windows programming.  Visual Basic encapsulates the Windows API into objects that Visual Basic programmers can manipulate.  For example, when you use the C programming language, creating a program that greets the user with the message " Welcome to Visual Basic" can take several lines of instructions.  In Visual Basic, however, the same program requires only a single line.

Share:

Trending

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