Ocx Command

Purpose

Creates an Ocx control in the current active form, window, or dialog.

Syntax

Ocx type name[(idx)] [[= text$] [,ID][, x, y, w, h] [, style]]

type:object typename
name:variable name (global)
idx:iexp, control array index
text$:sexp, caption (optional)
ID:iexp, identifier value for the control
x, y, w, h:iexp, position and dimension of the object
style:iexp, additional windows style constants

Description

Ocx is used to create an Ocx control in the source code, rather than in the Form Editor. Ocx takes at least two arguments: an Ocx type (OLE Control CoClass), and a variable name to which the object is assigned. The name represents the control in code and is a global variable of the given Ocx-type. For example, the following statement creates a Button control (Ocx type is Command) at position 10, 10 and with width = 80 and height = 24 pixels.

Ocx Command cmd = "Ok", 10, 10, 80, 24

The coordinates and size measurement are set with OcxScale. By default, the Ocx and OcxOcx commands use pixel coordinates. Setting OcxScale = 1 determines that the Ocx and OcxOcx commands use the ScaleMode setting of the form.

Some Ocx controls have a default position and size, either because they have a fixed position (ToolBar, StatusBar, TrayIcon) or they are invisible (ImageList, Timer, CommDlg). For instance:

Ocx ToolBar tb          // Aligned at top

Ocx StatusBar st        // Aligned at bottom

.SimpleText = "Ready"

Do : Sleep : Until Me Is Nothing

After an Ocx or OcxOcx command a hidden With command is active with the Ocx object just created. The With is valid until the next With or a new Ocx is created.

Once a global Ocx variable is entered in code its name is used for a kind of IntelliSense. By typing in the name followed by a dot, a context menu with possible properties and methods for that Ocx type is presented. By browsing through the list, the syntax of the property or method is displayed in the statusbar of the IDE. A selection is made by pressing ENTER, any other key closes the list. In the same way an event name can be selected. After typing 'Sub name_' a list pops up showing the possible event names for that control.

GFA-BASIC 32 supports all standard and common controls. The Ocx control types are: Animation, CheckBox, ComboBox, Command, CommDlg, Form, Frame, Image, ImageList, Label, ListBox, ListView, MonthView, Option, ProgressBar, RichEdit, Scroll, Slider, StatusBar, TabStrip, TextBox, Timer, ToolBar, TrayIcon, TreeView, UpDown.

A control array is a group of controls that share the same name and type. They also share the same event procedures. A control array has at least one element and can grow to as many elements as your system resources and memory permit. The maximum index you can use in a control array is 32767. Elements of the same control array have their own property settings.

Each control is referred to with the syntax object(index). You specify the index of a control when you create it. The Index property distinguishes one element of the control array from another. When one of the controls in the array recognizes an event, a common event procedure is invoked and the value of the Index property is passed to identify which control actually triggered the event.

Example

Form frm1 = "GFA-Test", 10, 10, 250, 170

Ocx Command cmd(1) = "OK", 30, 100, 45, 25

cmd(1).Default = True        ' implicit With

Ocx Command cmd(2) = "Cancel", 80, 100, 45, 25

cmd(2).Cancel = True   ' explicit reference

Ocx Command cmd(3) = "But_3", 130, 100, 45, 25

Do

Sleep

Until Me Is Nothing

 

Sub cmd_Click(Index%)

Local a$ = "Command Button " & Index% & #13#10 & Iif(Index% <> 3, "Click OK to close main window", "")

Message a$

If Index% <> 3 Then Me.Close

EndSub

Remarks

Normally, GFA-BASIC 32 assigns a control a unique identifier, but when porting GFA-BASIC 16 code to GFA-BASIC 32 it might be useful to assign a custom ID-value. For instance, porting the Button command to GFA-BASIC 32 requires at least the replacement of the 'Button' keyword with 'Ocx Command name ='. The ID argument may remain in the statement and used further down the program.

The order of control creation determines their Z-order and tab position. The last control created has the highest Z-order position. To bring other controls to the front when they ar overlapped by others, use the ZOrder method.

More complex Forms are to be created with the Form Editor, due to its finer tuning possibilities.

See Also

OcxOcx, OcxScale, OCX(), Form, Command, Option, CheckBox, RichEdit, ImageList, TreeView, ListView, Timer, Slider, Scroll, Image, Label, ProgressBar, TextBox, StatusBar, ListBox, ComboBox, Frame, CommDlg, MonthView, TabStrip, TrayIcon, Animation, UpDown

{Created by Sjouke Hamstra; Last updated: 20/10/2014 by James Gaite}