Introduction to VisualBasic controls
You can draw controls on a form using tools from the Visual Basic toolbox.
Control classification
There are three broad categories of Visual Basic controls:
1. Internal controls, such as CommandButton and Frame controls. These controls are in the Visual Basic .exe file. Internal controls always appear in the toolbox, unlike ActiveX controls and pluggable objects that can be added to or removed from the toolbox.
2. ActiveX control is an independent file with the extension .ocx, which includes controls provided by various versions of Visual Basic (DataCombo, DataList control, etc.) and controls only provided in the professional and enterprise versions (such as Listview, Toolbar, Animation and TabbedDialog), in addition to many ActiveX controls provided by third parties.
Note that controls with the file extension .vbx use older technology and can be found in applications written in earlier versions of Visual Basic. When Visual Basic opens a project that contains a .vbx control, it replaces the .vbx control with an .ocx control by default. Of course, this is only possible if the .ocx version of the control exists. For information about updating controls to .ocx format, see the "Updating Legacy Visual Basic Controls" section later in this chapter.
3. Insertable objects, such as a Microsoft Excel worksheet object containing a list of all employees of the company, or a Microsoft PRoject calendar object containing a certain project plan information. Because these objects can be added to the toolbox, they can be used as controls. Some of these objects also support automation (formally known as OLE Automation), which allows you to programmatically control objects of another application in a Visual Basic application. For more information about automation, see "Programming with Objects" in Chapter 10, "Programming with Components."
For more information about the ActiveX controls available in Visual Basic Professional and Enterprise editions, see "Using ActiveX Controls" in the Component Tools Guide.
internal controls
The following table summarizes the internal controls in the Visual Basic toolbox.
Note that the Pointer tool (the first tool in the toolbox) can be used to move and resize forms and controls. Pointer tools are not controls.
Standard ActiveX control
VisualBasic Learning Edition includes several ActiveX controls (called standard ActiveX controls) that allow you to introduce advanced functionality into your applications. ActiveX controls have a file extension of .ocx and can be manually added to the toolbox for use in projects. The following table summarizes the standard ActiveX controls provided by Visual Basic Learning Edition.
For more information about the ActiveX controls available in Visual Basic Professional and Enterprise editions, see "Using ActiveX Controls" in the Component Tools Guide.
Add and remove ActiveX controls
Follow the steps below to add or remove ActiveX controls from the toolbox.
To add ActiveX controls to the toolbox:
1. Select "Components" in the "Project" menu.
2. Select the check box next to the .ocx control name, and then select "OK." After placing the controls in the toolbox, add them to the form just as you would the internal controls.
To remove ActiveX controls:
1. Delete all instances of the control on the project form. Remove all references to the control in the project code. If a reference to the deleted control remains in the code, an error message will be displayed when the application is compiled.
2. Select "Components" in the "Project" menu. Clear the checkbox next to the .ocx control name, and then select OK. If there are instances of the control in the project, an error message will be displayed.
For more information about adding or removing controls and insertable objects in the toolbox, see "Adding Controls in a Project" in Chapter 4, "Management of Projects."
Updating legacy VisualBasic controls
Older 16-bit Visual Basic controls with a .vbx file extension are not compatible with this version of Visual Basic. If you try to load an old project that contains a .vbx control, Visual Basic will warn that the control is not applicable or compatible. At this time, you can continue to load the project without a .vbx control. Of course, the application will not run normally.
If you have an old version of Visual Basic project that contains a third-party .vbx control, please contact the control manufacturer for information about .ocx replacement controls.
Control naming convention
When you first create an object (form or control), Visual Basic sets its Name property to the default value. For example, initially set the Name property of all command buttons to Commandn, where n is 1, 2, 3, and so on. VisualBasic names the first command button drawn on the form Command1, the second Command2, and the third Command3.
You can keep the default name; however, if you have several controls of the same type, it is best to change the Name property to a descriptive name. Because it is difficult to distinguish the MyForm form's Command1 button from YourForm's Command1 button, the naming convention is helpful, especially when the application contains several forms, standards, and class modules.
You can name a control by using a prefix that describes the class, followed by a descriptive name for the control. Using a naming convention, your code can automatically describe itself and arrange similar objects alphabetically in an object list box. For example, you can name the CheckBox control like this:
chkReadOnly
Names for forms and controls:
1. Must start with a letter.
2. Can only contain letters, numbers and underscore characters (_); punctuation characters and spaces are not allowed.
3. Cannot exceed 40 characters.
For more information about naming conventions, see Appendix B, "Visual Basic Coding Conventions."
Use control value
All controls have a property. You can use this property to store or get a value by simply referencing the control name without using the property name. This property is called the value of the control. This is the most important or commonly used property of the control. The following table lists each control property, which is considered a control value.
When a control's property is the control's value, you don't have to specify the property name in code whenever you reference the property. For example, the following code sets the value of the Text property of the TextBox control:
Text1=ThistextisasignedtotheTextproperty_
ofText1
In the following example, whenever the user clicks a file in the file list box, the Caption property of Label1 is set to the FileName property of File1:
PrivateSubFile1_Click()
Label1=File1
EndSub
Note that the readability of the code is slightly affected by the use of control values, so the examples in this guide do not use control values but explicitly reference the properties of the control. Both methods can be used to write code, and consider using control values when they do not cause reading difficulties.
->