Creates an Ocx control with an Ocx parent in the current Form.
OcxOcx parocx[(c_idx)] ocxtype name[(idx)] [[= text$] [,ID][, x, y, w, h] [, style]]
parocx | : object variable name for the parent Ocx |
c_idx | : const iexp, control array index number |
ocxtype | : object typename |
name | : variable name (global) |
idx | : iexp, control array index number |
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 |
OcxOcx is used to create an Ocx control with some other Ocx control as its parent. OcxOcx takes at least three arguments: an Ocx variable name, an Ocx type (OLE Control CoClass), and a variable name to which the new Ocx object is assigned.
The parocx name represents the Ocx control that is to be the parent and name is the global variable name for the Ocx control in code. The parent Ocx parocx can be one of the following types:
parocx | Meaning |
---|---|
Form | A Form ocx can be used as a container (of course). |
Image | A container with a small resource footprint. This could be used instead of a Form, which uses more resources (scaling, a DC, a Picture). |
Frame | Particularly useful for Option Ocxes (RadioButtons). The Transparent property of the Frame may not be changed; otherwise, the embedded controls are invalid. |
TabStrip | To embed (for instance) a Frame Ocx. |
ToolBar | To embed (for instance) a ComboBox Ocx. |
StatusBar | To embed (for instance) a Command Ocx |
NOTE: parocx MUST be one of the above types and can not be defined as a generic Control; see Known Issues below.
The ocxtype specifies the control to create. OcxOcx can be used to create all supported Ocx types: 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.
The following statement creates a Button control (Ocx type is Command) at position 10, 10 and with width = 80 and height = 24 pixels in an (Ocx) Form.
OpenW 1
OcxOcx Win_1 Command cmd1 = "OK", 10, 10, 80, 24
.Default = True
.FontBold = True
Do : Sleep : Until Win_1 Is Nothing
After an Ocx or OcxOcx command has been executed, a hidden With command is active with the Ocx object just created. The With is valid to the next With or to the place a new Ocx is created.
Note: The OcxOcx command is also present in the context menu of the Form Editor (right button click on the control).
OpenW 1
' Load a bitmap
Ocx ImageList iml
.ListImages.Add , "I", CreatePicture(LoadIcon(Null, IDI_WARNING))
Ocx ToolBar tb
.ImageList = iml
// The first button is normal button, with picture "I"
Local btn As Button
Set btn = .Buttons.Add( , , , , "I")
Set btn = .Buttons.Add( ,"cb", , 4)
.Buttons("cb").Width = 100
OcxOcx tb ComboBox cb = , btn.Left, _
btn.Top, btn.Width, btn.Height * 8
Local i%
For i = 0 To 99
cb.AddItem Rnd, i
Next
Ocx ListBox lb = , 300, 0, 100, btn.Height * 8
For i = 0 To 99
lb.AddItem Rnd, i
Next
lb.Top = 0 ' Move the ListBox vertical below the ToolBar
Do
Sleep
Loop Until Me Is Nothing
Sub cb_Click
Print "cb_Click"
Print cb.ItemData(cb.ListIndex)
Print cb.Text
End Sub
Sub tb_Click
Print "tb_Click"
End Sub
Sub lb_Click
Print "lb_Click"
Print lb.ItemData(lb.ListIndex)
Print lb.Text
End Sub
Example – Using a control array.
OpenW 1
Const id_frame = 400 ' MUST BE A CONST!
Ocx Frame fra(id_frame) = "Colors" , 110, 40, 156, 164
Local Int idx = id_frame + 1
OcxOcx fra(id_frame) Option opt(idx) = "Border", 8, 020, 60, 20 : Inc idx
OcxOcx fra(id_frame) Option opt(idx) = "Label", 8, 040, 60, 20 : Inc idx
OcxOcx fra(id_frame) Option opt(idx) = "Fore", 8, 060, 60, 20 : Inc idx
Do
Sleep
Until Me Is Nothing
When using the control array syntax for the OcxOcx parent, the index must be of type Const Int, see the example above.
Ocx creates a control whose parent is Me. Therefore, Ocx is the same as OcxOcx Me.
OpenW 1 : TitleW 1, " Win 1"
OpenW 2 : TitleW 2, " Win 2"
' create a button in Win 1:
Set Me = Win_1 :
OcxOcx Me Command cmd1 = "GFA", 10, 10, 50, 20
' Button in current active window (Me)
Ocx Command cmd2 = "GFA2", 10, 40, 50, 20
Do
Sleep
Loop Until Win_1 Is Nothing
CloseW 2
OpenW 1
OcxOcx Win_1 TextBox txt1 = "TextBox 1", 10, 10, 200, 20
OcxOcx Win_1 Command cmd = "Add TextBox", 10, 40, 100, 25
Do : Sleep : Until Win_1 Is Nothing
Sub cmd_Click
TextChange1
TextChange2( Win_1)
EndSub
Function TextChange1
OcxOcx Win_1 TextBox txt2 = "TextBox 2", 10, 70, 200, 20
EndFunction
Function TextChange2(ByRef parocx As Form)
OcxOcx parocx TextBox txt3 = "TextBox 3", 10, 100, 200, 20
EndFunction
If the above example is run, on every second run the IDE will query the validity of parocx in TextChange3; every other time it will run as expected. There is currently no workaround for this. (Interestingly, if the above code is compiled and run, it works every time.)[Reported by Roger Cabo, 01/06/2022]
Ocx, OCX(), Me, 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: 15/06/2022 by James Gaite; Other Contributors: Jean-Marie Melanson}