Example program - Using OBJECT notation on forms
This example program shows how to use OBJECT notation to controls grids and trees. OBJECT notation can also be used with tabs and list boxes.
In the forms Enter() event the grid is populated and some cells are colored. OBJECT notation is used here for the form, the grid control and for a particular cell on the grid. Objects can be passed to and returned from subroutines as exemplified in the 'CellText() routine. Note that this subroutine has a default parameter, a feature introduced in KCML 5.02.
Also in the Enter() event a three level tree is constructed using Add() methods and its first and thirst levels expanded using Expand(). The 'BoldTree() subroutine recursively descends the tree from the top set bold attributes on all the elements and setting the node icon to either a folder open icon if a node has children or to a stop sign icon if it is a leaf.
Finally there is an example of using FOR OBJECT to iterate over the collect of all the controls on a form. If the 3rd button, the help button which is labelled 'click me', is pressed, its event handler will iterate over each control in turn using FOR OBJECT and change the color and text of the control, provided it is a button, verified by checking its id to be between 1 and 9.
REM Form object demonstration REM REM This program highlights many of features available REM when using the OBJECT syntax with forms programming. REM DEFFORM formobj()={.form,.form$,.Style=0x50c000c4,.Width=273,.Height=210,.Text$="Form objects demonstration",.Id=1024},\ {.ok,.button$,.Style=0x50010001,.Left=217,.Top=6,.Width=50,.Height=14,.Text$="OK",.__Anchor=5,.Id=1},\ {.cancel,.button$,.Style=0x50010000,.Left=217,.Top=23,.Width=50,.Height=14,.Text$="Cancel",.__Anchor=5,.Id=2},\ {.help,.button$,.Style=0x50010000,.Left=217,.Top=44,.Width=50,.Height=14,.Text$="&Click me",.__Anchor=5,.Id=9},\ {.gridControl1,.kcmlgrid$,.Style=0x50010430,.Left=15,.Top=10,.Width=170,.Height=61,.Id=1000,.Rows=5,.Cols=5,.Col1={.Row=0,.Col=1,.ColWidth=53}},\ {.treeControl1,.tree$,.Style=0x50810007,.Left=15,.Top=84,.Width=170,.Height=113,.Id=1001,.HasPictures=1},\ {.textControl1,.static$,.Style=0x50000000,.Left=195,.Top=86,.Width=71,.Height=85,.Id=1002},\ {.cmdButton1,.button$,.Style=0x50010000,.Left=197,.Top=183,.Width=66,.Height=19,.Text$="Delete node",.Id=1003,.Click()} LOCAL DIM OBJECT t3 DEFEVENT formobj.Enter() LOCAL DIM OBJECT f, OBJECT c, OBJECT cell, OBJECT m, OBJECT t1, OBJECT t2 REM Get an object reference to a form, a control and a grid cell. OBJECT f = formobj OBJECT c = formobj.gridControl1 OBJECT cell = formobj.gridControl1.Cell(1, 1) REM Use the cell object to set some properties. cell.Text$ = "Cell(1,1)" cell.BackColor = &.Red cell.TextColor = &.Yellow REM Use a different cell now OBJECT cell = formobj.gridControl1.Cell(2, 1) REM Pass the cell to a subroutine. We are only passing one argument this time. 'celltext(OBJECT cell) REM Pass two cells two the same subroutine. 'celltext(OBJECT c.Cell(3, 1), OBJECT c.Cell(1, 1)) REM Now get back the middle cell of a grid from a subroutine. OBJECT m = 'middlecell(OBJECT c) m.Text$ = "Middle" REM Now have a look at trees. OBJECT c = f.treeControl1 OBJECT t1 = c.Add("Root", 0) OBJECT t2 = t1.Add("Child", 0) OBJECT t3 = t1.Add("Child2", 0) t2.Add("One", 0) t2.Add("Two", 0) t3.Add("Four", 0) t3.Add("Five", 0) REM expose first level t1.Expand() t3.Expand() t3.Text$ = "Revised child2" REM Set every item of the tree to bold 'boldtree(OBJECT t1) REM Some of the methods for tree item objects t2.Select() t3.Collapse() END EVENT DEFEVENT formobj.help.Click() REM Set all default button controls (OK, Cancel, Help) to have red text REM these buttons have special reserved magic ID numbers FOR OBJECT c IN formobj IF (c.Id >= 1 AND c.Id <= 9) a$ = c.Text$ c.Text$ = a$ & " (id = " & c.Id & ")" c.TextColor = &.Red END IF NEXT OBJECT c REM and disable ourselves ..Enabled = FALSE END EVENT DEFEVENT formobj.cmdButton1.Click() REM drop child 2 IF (OBJECT t3 <> NULL) t3.Delete() OBJECT t3 = NULL END IF ..Enabled = FALSE END EVENT FORM END formobj formobj.Open() END DEFSUB 'celltext(OBJECT cell, OBJECT copy = NULL) REM If copy is specified then copy the text to cell, else use a NULL text. IF (OBJECT copy <> NULL) cell.Text$ = "Copy of " & copy.Text$ ELSE cell.Text$ = "NULL" END IF END SUB DEFSUB 'middlecell(OBJECT grid) REM Return the middle cell of a grid as an object. LOCAL DIM OBJECT cell OBJECT cell = grid.Cell((grid.Rows - 1) / 2 + 1, (grid.Cols - 1) / 2 + 1) RETURN OBJECT cell END SUB DEFSUB 'boldtree(OBJECT c) REM Recursively set each item to bold. LOCAL DIM OBJECT child WHILE (OBJECT c <> NULL) DO c.Bold = TRUE OBJECT child = c.Child IF (OBJECT child <> NULL) REM node has a child c.Image = &.FolderClosed c.OpenImage = &.FolderOpen REM recurse over children 'boldtree(OBJECT child) ELSE REM leaf node, mark with stop sign c.Image = &.Stop c.OpenImage = &.Stop END IF OBJECT c = c.Next WEND END SUB