Home

Introduction to Controls

 

Controls Fundamentals

 

Introduction

By itself, a form means nothing. Its role is revealed in the objects it holds. You can add such objects to a form or the body of a spreadsheet.

   

Practical LearningPractical Learning: Introducing Properties

  1. Start Microsoft Excel
  2. On the Ribbon, click Developer
  3. In the Code section, click Visual Basic

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.

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. To create a form, on the main menu, click Insert -> UserForm
  2. To access its associated module, right-click the form and click View Code
  3. To return to the form, on the main menu, click Window and click the menu item that has (UserForm).
  4. In Microsoft Visual Basic, notice that a Toolbox appears next to the form
     
    Microsoft Visual Basic
  5. To return to Microsoft Excel, on the Taskbar, click Microsoft Excel
  6. 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    
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    

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.

To programmatically add a control to a spreadsheet, use the following formula:

Private Sub Exercise()
    Worksheets(1).OLEObjects.Add "Forms.ControlName.1"
End Sub

The only thing you need to know and change about this formula is the ControlName factor. We will learn about Worksheets(1) in Lesson 12. Use the following names:

Use this Name To Get a   Use this Name To Get
CheckBox Check Box   ComboBox Combo Box
CommandButton Command Button   Label Label
ListBox List Box   Image Image
OptionButton Option Button   ScrollBar Scroll Bar
SpinButton Spin Button   TextBox Text Box
ToggleButton Toggle Button      

Here is an example that creates and positions a text box on a spreadsheet:

Private Sub Exercise()
    Worksheets(1).OLEObjects.Add "Forms.TextBox.1"
End Sub

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 work area 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
 
 
   

Previous Copyright © 2004-2009 FunctionX Next