Home

Introduction to Objects

 

Fundamentals of Windows Controls

 

Introduction

A computer application, such as those that run on Microsoft Windows, is equipped with objects called Windows controls. These are the objects that allow a person to interact with the computer.

The Form

The primary control used on most applications is called a form. To create a form, on the main menu of Microsoft Visual Basic, you can click Insert -> UserForm. This would add a form to your project.

The form is primarily used as a platform on which you add other controls. For this reason, a form is referred to as a container. By itself, a form is not particularly useful. You should add other objects to it.

When you create or add a form, a module is also automatically created for it. To access the module associated with a form, you can right-click the form and click View Code.

 

The Spreadsheet

When working in Microsoft Excel, you can use Windows controls either on the work area or in a form in Microsoft Visual Basic. Therefore, just like a form, a spreadsheet also is a container of controls.

Practical LearningPractical Learning: Creating a Form

  1. Start Microsoft Excel and, on the Ribbon, click Developer
  2. In the Code section, click Visual Basic
  3. To create a form, on the main menu, click Insert -> UserForm
  4. To access its associated module, right-click the form and click View Code
  5. To return to the form, on the main menu, click Window and click the menu item that has (UserForm)

Introduction to Windows Controls

The main objects used to help a person interact with the computer are Windows controls. There are two main ways you can access these objects when working with Microsoft Excel.

If you are working in Microsoft Excel, you can add or position some Windows controls on the document. To do that, on the Ribbon, click Developer. In the Control section, click Insert:

Insert

This would display the list of controls available in Microsoft Excel. The controls appear in two sections: Form Controls and ActiveX Controls. If you are working on a spreadsheet in Microsoft Excel, you should use only the controls in the ActiveX Controls section. If you are working on a form in Microsoft Visual Basic, a Toolbox equipped with various controls will appear.

Practical LearningPractical Learning: Accessing Windows Controls

  1. In Microsoft Visual Basic, notice that a Toolbox appears next to the form
     
    Microsoft Visual Basic
  2. To return to Microsoft Excel, on the Taskbar, click Microsoft Excel
  3. To display the controls, in the Controls section of the Ribbon, click Insert

Using Additional Objects

The Developer tab of the Ribbon in Microsoft Excel provides the most regularly used controls. These controls are enough for any normal spreadsheet you are developing. Besides these objects, other controls are left out but are still available. To use one or more of these left out controls, in the Controls section of the Ribbon, click Insert and click the More Controls button:

More Controls

This would open the More Controls dialog box:

You can scroll up and down in the window to locate the desired control. If you see a control you want to use, click it and click OK.

In Microsoft Visual Basic, to access more controls, on the main menu, you can click Tools -> Additional Controls... This would open the Additional Controls dialog box:

Additional Controls

To select a control, you can click its check box. After selecting the controls, click OK.

The Names of Controls

Every control in the Developer tab of the Ribbon or in the Toolbox of Microsoft Visual Basic has a specific name. You may be familiar with some of these controls. If you are not sure, you can position the mouse on a control and a tool tip would come up. In our lessons, we will use the tool tip of a control to name it. The names we will use are:

ActiveX Controls Name Forms Controls Name
Button Command Button Label Label
Combo Box Combo Box Toggle Button Toggle Button
Check Box Check Box Label Label
List Box List Box TabStrip
Text Box Text Box MultiPage
Scroll Bar Scroll Bar Scroll Bar ScrollBar
Spin Button Spin Button Text Box
Option Button Option Button Image
Label Label RefEdit
Image Frame
Toggle Button Toggle Button Group Box Group Box

Adding a Control to a Container

 

Adding One Control

To use one of the controls from the Ribbon or the Toolbox, you can click it. If you then simply click its container, the control would be added where you clicked and with some default dimensions.

In Microsoft Excel, if you click the button control in the Form Controls section and click the work area, just after the control has been added the Assign Macro dialog box would come up:

Assign Macro

In our lessons, when working in Microsoft Excel, we will avoid using the objects in the Form Controls section.

If you want, instead of clicking and releasing the mouse, you can click and drag. This allows you to "draw" the control and give the dimensions of your choice. If the control has already been added but you want it to assume different dimensions, you can click it to select it and then drag one of its border handles.

Adding Many Controls

The above technique is used to add one control at a time. If you want to add the same control again, you must click it on the Ribbon or in the Toolbox and click its container again. If you plan to add the same control many times, in the Toolbox of Microsoft Visual Basic, double-click the control and click the form as many times as necessary. When you have reached the desired number of copies of that control, to dismiss it, in the Toolbox, click the same control again, click another control, or click the Select Objects button.

Practical Learning Practical Learning: Accessing Windows Controls

  1. To add a control to the document, Under ActiveX Controls, click the Command Button button Command Button and click a box (any box in the work area)
  2. To add another control, in the Controls section of the Ribbon, click the arrow under Insert and click the Combo Box button Combo Box and click somewhere in the work area away from the previously added button
  3. On the Taskbar, click Microsoft Visual Basic to return to it
  4. On the Toolbox, click the CommandButton and click somewhere in the top-left section of the form (no need for precision at this time)
  5. On the Toolbox, click the ComboBox and click somewhere in the middle-center section of the form (no need for precision at this time)
  6. On the Toolbox, click the CheckBox and click somewhere in the lower-right section of the form (no need for precision at this time)
  7. To return to Microsoft Excel, click the View Microsoft Excel button View Microsoft Excel

Control Selection

 

Single Control Selection

After you have added a control to a container, in order to perform any type of configuration on the control, you must first select it. Sometimes you will need to select a group of controls.

To select a control in the work area in Microsoft Excel, first, in the Controls section of the Ribbon, click the Design Mode button Design Mode. After clicking it, right-click the control and press Esc. If you are working in Visual Basic, to select a control, click it on the form.

In Microsoft Excel, when a control is selected, it is surrounded by 8 small circles, also called handles. Here is an example:

Selected Control

In Microsoft Visual Basic, when a control is selected, it is surrounded by 8 small squares:

A Selected Control on a Form

Practical Learning Practical Learning: Selecting Controls

  1. Position the mouse on CommandBut that was positioned on the form and click.
    Notice that you are able to select the button
  2. In the Controls section of the Ribbon, click the Design Mode button Design Mode
  3. In the work area, click the CommandBut button again
  4. Click the combo box. Notice that, this time, you cannot select the controls
  5. To return to controls to edit mode, in the Controls section of the Ribbon, click the Design Mode button Design Mode
  6. To return to Microsoft Visual Basic, in the Code section of the Ribbon, click the Visual Basic button Visual Basic

Multiple Control Selection

To select more than one control on a spreahsheet, click the first. Press and hold Shift. Then click each of the desired controls. If you click a control that should not be selected, click it again. After selecting the group of controls, release Shift:

Controls Selected

If you are working on a form in Microsoft Visual Basic, first click one of the controls you want to select, then press and hold Ctrl. Click each of the desired controls. If you click a control that should not be selected, click it again. After selecting the group of controls, release Ctrl that you were holding. As another technique you can use to select various controls, click an unoccupied area on the form, hold the mouse down, drawing a fake rectangle that would either include each of the desired controls or would touch each, then release the mouse:

Selecting Controls

Every control touched by the fake rectangle or included in it would be selected:

Selected Controls

When a group of controls is selected, the first selected control displays 8 handles but its handles are white while the others are dark.

Practical LearningPractical Learning: Selecting and Using Various Controls

  1. On the form, click one of the controls
  2. Press and hold Ctrl
  3. Click one of the other controls
  4. Release Ctrl
  5. To dismiss the selection, press Esc

Control Deletion

If there is a control on your form or your workarea but you don't need it, you can remove it. To delete a control, first select it and then press Delete. You can also right-click a control and click Cut.

To remove a group of controls, first select them, then press Delete or right-click the selection and click Cut.

Practical LearningPractical Learning: Deleting Controls

  1. While still in Microsoft Visual Basic, press Ctrl + A to select all controls
  2. Press Delete to remove them
  3. To display them again, press Ctrl + Z
  4. To return to Microsoft Excel, click the View Microsoft Excel button View Microsoft Excel

Classes and Objects

 

Introduction

The language of Microsoft Visual Basic uses the concept of class to identify or manage the parts of an application. Consider an object like a house. It has such characteristics as its type (single family, townhouse, condominium, etc), the number of bedrooms, the number of bathrooms, etc:

Single Family House

These characteristics are used to describe a house to somebody who wants to buy it. To get such an object, you must first define the criteria that describe it. Here is an example:

House
[
      Address
      Type of House
      Number of Bedrooms
      Number of Bathrooms
      Has Indoor Garage
      The Living Room is Covered With Carpet
      The Kitchen Has an Island Stove
]

This information is used to describe a house. Based on this, House is called a class. To actually describe a real house, you must provide information for each of the above characteristics. Here is an example:

House: Langston
[
      Address: 6802 Leighton Ave
      Type of House: Single Family
      Number of Bedrooms: 4
      Number of Bathrooms: 3
      Has Indoor Garage: Yes
      The Living Room is Covered With Carpet: Yes
      The Kitchen Has an Island Stove: No
]     

In this case, Langston is not a class anymore, it is a real house and is explicitly described. Therefore, Langston is called an object. Based on this, a class is a technique used to provide the criteria to define an object. An object is the result of a description based on a class.

Properties

In our example of a house, we used words to describe it. Examples are: Address, Type of House, Number of Bedrooms, Number of Bathrooms. In computer programming, the characteristics used to describe an object are referred to as its properties.

To display the characteristics of a Windows control, in Microsoft Excel:

  • Right-click the control and click Properties:

Properties

  • If the control is selected in the work area, in the Controls setion of the Ribbon, click the Properties button Properties.

Any of these two actions would display the Properties window for the control that was right-clicked:

Properties Window

The Properties window would stay on the screen of Microsoft Excel as long as you want. To show the properties of another control, simply click it in the work area.

If you are working in Microsoft Visual Basic, to show the characteristics of a control, right-click it and click Properties. This also would display the Properties window and show the characteristics of the selected control. While the Properties window in Microsoft Excel floats and does not hold a specific position, by default, in Microsoft Visual Basic, the Properties window is position on the lower-left side:

The Properties Window in Microsoft Visual Basic

You can move it by dragging its title bar.

The Properties window allows you view or change a characteristic of the control. The properties of an object can be changed when designing it or by writing code. You can also manipulate these characteristics both at design and at run times. This means that you can set some properties at design time and some others at run time.

The items in the Properties window display in two lists. In the beginning, you may be regularly lost when looking for a particular property. To get the list in alphabetic order, click the Alphabetic tab. To get the list by categories, click the Categorized tab.

When a control is selected, the Properties window displays only its characteristics. When various controls have been selected, the Properties window displays only the characteristics that are common to the selected controls.

Practical Learning Practical Learning: Viewing the Properties of a Control

  1. To view the properties of the button, in the work area, right-click the CommandBut button you added and click Properties
  2. On the Taskbar, click Microsoft Visual Basic
  3. To see the properties of a Visual Basic object, right-click an unoccupied area of the form and click Properties.
    Notice that the Properties window displays the characteristics of the form
  4. To see the properties of another object, on the form, click the button

Introduction to the Properties Window

 

Introduction

After you have added a control to a work area or to a form, in order to perform any type of configuration on the control, we saw that you must first select it. Besides the techniques we reviewed, to select a control, if you know its name, you can click the arrow of the combo box in the top section of the Properties window and select it:

Selecting a Control Using the Properties Window

The Name of a Property

After adding a control to your application, you can manipulate its characteristics. If you are working in Microsoft Excel, to put a control into edit mode, in the Controls section of the Ribbon, click the Design Mode button Design Mode.

Each field in the Properties window has two sections: the property’s name and the property's value:
 

Properties Names and Values

The name of a property is represented in the left column. This is the official name of the property. Notice that the names of properties are in one word. Based on this, our House class would have been defined as follows:

House
[
      Address
      TypeOfHouse
      NumberOfBedrooms
      NumberOfBathrooms
      HasIndoorGarage
      LivingRoomCoveredWithCarpet
      KitchenHasIslandStove
]

You can use this same name to access the property in code. 

Accessing a Control's Property

To access a property of a control using code, type the name of the control, followed by a period, followed by the name of the property. Based on this, if you have a House object named Langston, to access its TypeOfHouse property, you would write:

Langston.TypeOfHouse

The Value of a Property

The box on the right side of each property name represents the value of the property that you can set for an object. There are various kinds of fields you will use to set the properties. To know what particular kind a field is, you can click its name. To set or change a property, you use the box on the right side of the property’s name: the property's value, also referred to as the field's value.

The Default Value of a Control's Property

To programmatically change the value of a property, type the name of the control, followed by a period, followed by the name of the property, followed by =. Then, on the right side of equal, you must provide the value but this depends on the type of value.

The people who developed the controls also assigned some primary values to their properties. This is the type of value that a property either is most likely to have or can use unless you decide to change it. The primary value given to a property is referred to as its default value. Most of the time, you can use that property. In some other assignments, the default value will not be suitable.

Types of Properties

 

Empty Fields

Empty Field  

By default, these fields don't have a default value. Most of these properties are dependent on other settings of project.

To set the property on such a field, you can type in it or sometimes you will need to select from a list.

 

Practical LearningPractical Learning: Viewing Empty Property Fields

  1. On the form, click the CommandButton1 button
  2. In the Properties window, click ControlTipText and notice that its value is empty

Text Fields

There are fields that expect you to type a value. Most of these fields have a default value.

To change the value of the property, click the name of the property, type the desired value, and press Enter or Tab. While some properties, such as the Caption, would allow anything, some other fields expect a specific type of text, such as a numeric value.

To programmatically change the value of a text-based property, on the right side of the = sign, you can type the value in double quotes. For example, suppose you have a House object named Langston. If you want to specify its address, you would write:

Langston.Address = "6802 Leighton Ave"
Text Field
 

Practical LearningPractical Learning: Viewing Text Fields

  1. Click an unoccupied area of the form
  2. In the Properties window, click Caption type Application Design

Numeric Fields

Some fields expect a numeric value. In this case, you can click the name of the field and type the desired value. If you type an invalid value, you would receive a message box notifying you of the error:

Value Error

When this happens, click OK and type a valid value. If the value is supposed to be an integer, make sure you don't type it with a fractional part.

 

Practical LearningPractical Learning: Viewing Text Fields

  1. On the form, click the CommandButton1 button
  2. In the Properties window, click Left, type 32 and press Enter

Expandable Fields

Expandable Field

Some fields have a - or a + button. This indicates that the property has a set of sub-properties that actually belong to the same property and are defined together. To expand such a field, click its + button and a – button will appear.

To collapse the field, click the – button. Some of the properties are numeric based. With such a property, you can click its name and type the numeric value. Some other properties are created from a sub-list. If you expand such a field, it would display various options. With such a property, you should select from a list.

 

Practical LearningPractical Learning: Viewing Expandable Fields

  • Click an unoccupied area of the form.
    In the Properties window, click Categorized. If the Misc field has +, click it to expand

Boolean Fields

Some fields can have only a True or False value. These are Boolean fields. To change their value, you can either select from the combo box or double-click the property to switch to the other value.

To programmatically specify the value of a Boolean property, on the right side of the = symbol, type True or False. Here is an example:

Langston.HasIndoorGarage= True
 

Boolean Field

 

 

Practical LearningPractical Learning: Viewing Boolean Properties

  1. On the form, click the CommandButton1 button
  2. In the Properties window, click AutoSize and notice that its has a False value

Intermediary Fields

Intermediary

Some fields use a value that can be set through an intermediary action. Such fields display a browse button Browse. When you click the button, a dialog box would come up and you can set the value for the field.

 

Practical LearningPractical Learning: Viewing Intermediary Fields

  1. Click an unoccupied area of the form
  2. In the Categorized section of the Properties window, if the Font field has +, click to expand
  3. Under Font, click the Font field and notice that it has a browse button

List-Based Fields

To change the value of some of the fields, you would first click the arrow of their combo box to display the available values. After clicking the arrow, a list would display:

Property Selection

There are various types of list-based fields. Some of them display just two items. To change their value, you can just double-click the field. Some other fields have more than two values in the list. To change them, you can click their arrow and select from the list. You can also double-click a few times until the desired value is selected. Some other items would dispplay a window from where you would click the option you want:

Property Selection

To programmatically specify the value of a list-based property, you must use one from a list. For example, suppose you had defined a list of types of house as tpeSingleFamily, tpeTownHouse, and tpeCondominium. To use one of these values for a House object named Langston, you would type:

Langston.TypeOfHouse = tpeSingleFamily

In most cases, each member of such a list also uses a natural number. An example would be:

TypeOfHouse Value
0 tpeSingleFamily
1 tpeTownHouse
2 tpeCondominium

Although we used 0, 1, and 2 in this list, there are no predefined rules as to the number allocated for each member of the list. The person who created the list also decided what number, if any, each member of the list would have (if you are curious, in most programming languages or libraries, these types of properties are created using an enumeration (in C++ or in the the .NET Framework) or a set (Borland VCL)). Based on this, the above code would also be written as:

Langston.TypeOfHouse = 0

Practical LearningPractical Learning: Viewing Expandable Fields

  1. Click the CommandButton1 button on the form.
    In the Categorized section of the Properties window, if the Misc field has +, click it to expand
  2. Under Misc, click the MousePointer field and notice that it has a combo box button.
    Click the arrow of the combo box to see the list
  3. To close Microsoft Excel, click the Office Button and click Exit Excel
  4. When asked whether you want to save the changes, click No
 
 

Previous Copyright © 2008 Yevol Next