Tech Note 02: More about Controls

February 07, 2005

 

© NS BASIC Corporation. All rights reserved.

NS Basic/Desktop's ability to easily use controls and automation objects is one of its most powerful features. There are literally thousands of controls available, for virtually any conceivable purpose. As well, applications such as Word, Excel and Outlook can be automated from NS Basic/Desktop.

There are several types of controls, and different ways of using them in projects. This document will try to explain the differences and let you know how to use them.

  1. Intrinsic Controls
  2. Built In Controls and Collections
  3. ActiveX Controls
    3.1 Microsoft's VB ActiveX Controls
  4. Adding a Control to your Application
    4.1 Adding a control to your project from the Toolbox
    4.2 Adding a control to your project using AddObject
  5. Object Automation
  6. Object Browsing
  7. Licensing

1. Intrinsic Controls

We've put the basic controls that almost every app can use right into NS Basic/Desktop itself. These are the easiest to use: no extra libraries need to be installed with your app. Everything that is needed will be in your exe after you compile your program.

For documentation on Intrinsic controls, see the NS Basic/Desktop Handbook, or look in the Online Language Reference. Intrinsic controls can be added to forms in NS Basic/Desktop programs by selecting them from the ToolBox and placing them on the form. If you have a formless app, use the AddObject statement.

Here is a list of the Instrinsic Controls:

Have a look at the IntrinsicObjects.nsd sample to see all these controls in action.

2. Built In Controls and Collections

There are a number of controls that are built into VBScript itself and can be used by NS Basic/Desktop programs.

You can find good documentation about these objects on Microsoft's site or at DevGuru. To use one of these controls in your app, use the AddObject statement.

Here is a list of the VBScript Controls and Collections:

Have a look at the FileSystemObject.nsd sample to see many of these in action.

3. ActiveX controls

Controls that contain Type Libraries that describe the public interfaces can usually be accessed. However, since there is no requirement that a control have a Type Library and anybody can write an ActiveX control, there are differences in the level of documentation (internal and external) as well as how well they are implemented. We'll keep a list of the ones that work well: if you have found one you like, send an email to support@nsbasic.com so we can add it to the list.

To add a control to the NS Basic/Desktop ToolBox, use Tool...ActiveX Control Manager. If the control is well behaved, the properties of a control will be listed in the Property Picker when an object is highlighted on a form. This won't work with all controls, however: if they are not well behaved you will need to use AddObject to add the control to your project. This will make little or no difference in how the control runs: it only makes a difference in the IDE.

When distributing an app which uses an ActiveX control, you'll have to install the control on the user's system if it is not there already. Be sure to check the license agreements of any controls you redistribute to make sure you are allowed to do so.

After a control is installed on a system, it needs to be registered. This only has to be done once, and is usually done by the installer program. Otherwise, controls can be registered manually by doing [Start]...[Run] and typing

   regsvr32 myNewControl.dll

A handy tool for managing ActiveX controls is available, called the ActiveX Manager. It provides right-click support within Windows Explorer for easy registering and unregistering of ActiveX objects.

Have a look at the WinsockForm.nsb sample to see a program which uses the MSWinSock.ocx ActiveX control to read a web page over the net.

3.1 Microsoft's VB ActiveX controls

Only developers with a valid design-time license for these controls (such ComDlg32.ocx or MSWinsck.ocx) may distribute them for use in their applications. The control and design-time license can be obtained by downloading the VB 5.0 Control Creation Edition (VBCCE), purchasing VB 6.0, or installing a VB6 app that uses the controls. If you use them in an unlicensed environment, you will get a "Cannot create an object of that type - Error 80040112" message or Error number -214722123.

4. Adding a Control to your Application

4.1 Adding a Control to your Project from the ToolBox

If you are creating a standard app (with a form), you can add objects to your form from the ToolBox. (If you are creating a formless app, you will need to use AddObject.) Simply select the icon of the object from the icons in the ToolBox, and click on the form where you want it to be placed. Resize it then to the size you want.

If the control you want to use is not in the ToolBox, it can be added. Either right click in the ToolBox or select "ActiveX Control Manager" from the Tools Menu. When the dialog box comes up, click on Add and navigate to the control you wish to add. If it is well behaved, its icon will be added to the ToolBox. (If it isn't, you will have to use AddObject to add the control).

4.2 Adding a Control to your Project using AddObject

The AddObject statement allows you to add an object to your program at any point. Documentation on the actual syntax of the statement is in the NS Basic/Desktop Handbook and in the Online Help.

The only tricky part of using this statement can be the first argument, ObjectID. This is a string that is unique to each control. A control may have more than one ObjectID: it is essentially more than one control in a single file. If the Object ID is not listed in the documentation for the control, there are a couple of other ways to get this information.

5. Object Automation

One of the most powerful features of NS Basic/Desktop is its ability to automate COM based apps, such as Word, Excel and Outlook. You can use CreateObject (or related command) to create an object that refers to the application you want to automate.

Microsoft has extensive documentation on automation in MSDN.

To see an example of automation in action, check out the xls2csv sample that comes with NS Basic/Desktop. It shows how to open an Excel spreadsheet, extract the data and write it out to a .csv file.

6. Object Browsing

To use a control or automate an application, you have to know what interfaces it has. You cannot count on documentation being available (or complete). A better way to get this information is use an object browser, which examines the actual control or application to get this information.

If you would like to have a look at the internal documentation of an ActiveX control, there is a handy utility called ActiveX Documenter.

If you have Word or Excel, you can use Microsoft's excellent Object Browser. Hit Alt-F11 while in the application to see what is available in the app; then F2 to see the Object Browser. (This is the same Object Browser that is in VB6.)

You can also use OLEView. It's another object viewer utility from Microsoft. It lets you browse, configure, test and activate any COM class on your system. It's a free download from the MS site.

7. Licensing

All third party controls belong to someone. They will almost always have restrictions on how you can use them in your projects and under what conditions you may redistribute them to your customers. It's wise to make sure that you follow these conditions when you use them. NS Basic/Desktop has no way of checking whether you are using a control in a legal manner.