parentChild

Parent - Child programming. How to develop Layered Cascading programs using the RUN statement.

Small systems are simple. However as systems get larger it is almost impossible to write the entire system in one program. The best way to break up your applications into individual functions is to use the **RUN** statement.

The Run statement lets you RUN another program. This is the concept of Parent - Child relationships. The Parent can RUN any number of Child programs. The Child can be the Parent of other Children. You basically have a one to many relationship with one program running many others. This can cascaded for as many levels as you like.

Note: there are ways for the child to control the actions of the parent. This will be discussed later.. Bugs: You cannot use the UPLOAD command in a child program.

A typical scenario is a menu system running a list of programs. You have a Parent - the menu - with a selection of programs (child) that can be run. For example you could have a menu program for buttons to: [Maintenance] [Purchase orders] [Customers] When the user clicks the [Maintenance] button you want to clear the screen and **RUN** it. Once in the Maintenance program, how do you get back to the Parent Menu. Fortunately there is a way.

How the example works below: You can pass information around using UserInfo$. Usually you pass stuff like the user number, maintenance privileges and other information needed to run the child.. In the example below the parent [Exit App] button lets you exit the Child program when Clicked from the Parent. If you don't want the user to click the [Exit App] button then set the abort$ switch to 'N'o and the button will be invisible to the user.

Many times you want to control the Exit of the Child from within the Child program itself. Fortunitely you can have the Child program click the [Exit.App], even though you cannot see the button.

If abort$ is set to "Y"es then the [Exit App] button is visible and you can abort the child by clicking the button. if abort$ is set to "N"o the [Exit App] button is not visible. You then click the [Exit] button in the child. The [Exit] code finds reference of the invisible [Exit App] button and using code clicks the [Exit App] button. In any event once the [Exit App] key is pressed the parent program goes to wherever you want. Usually it clears the screen and displays the Menu again.

code ' at begining of menu set the cssclass for hide CSSClass ".hide", "{visibility: hidden; height:0px; bprder:none}"

' incdir$    -- this is the directory where the included Run program is located. It can be anywhere ' prog$      -- this is the name of the program you want to run ' runProg$   -- this is the name of the program that ran the Child. '               Sometimes you need to know who is the Parent because you may use the same program to do different '               things depending who RAN it ' UserInfo$   -- information passed to the Child. ' abort$     -- [Y]es the Parent can abort the Child.. [N]o it can not

' --- '     the Parent RUN program routine ' --- [mntRun] if prog$ = "" then            ' oh oh - forgot to gimme a program to run print "No program to run" goto [main] end if cls                     ' Clear the menu screen - Sometimes you do not want to clear the screen. ' For example you will not clear a invoice header to a child invoice items runProg$ = "menu"       ' this is who is running the RUNNED program runProgData$ = " "      ' you can pass anything you want here UserInfo$ = loginUserNum;" |";loginUserName$;" |";loginCanMaint$;" |";loginKeyKeeper$;" |";runProg$;" |";runProgData$;" |Y |"

prog$ = prog$ + ".bas"  ' put the .bas extension on the program name

button   #rtna, "Exit App" ,[main]      ' this is the [Exit App] program in the parent if abort$ = "N" then #rtna cssclass("hide")        ' If you don't want the parent to see the [Exit App] button then set abort$ = "N" end if   #rtna setid("exitApp")         ' set the id to "exitApp" - The child needs to use the "exitApp" id in the child code

incdir$ = DefaultDir$ + "\projects\child_project\" ' this is project where the Child program is located - in this case "child" run incdir$;prog$,#include            ' run the child program render #include                       ' render the child program

wait ' -- code The Child Program.. In the child program, you can gather the infromation passed in UserInfo$ Because of the way this is set up, you can run the Child program directly from RunBasic or from a Parent. The runPass$ switch tells the child if it was run from a Parent with Pass information. If it is [Y]es that means a Parent program ran it. The Child can also be the Parent of other Children. code ' - ' Get RUN pass information ' You can pass anything you want in UserInfo$ '  this is my standard. ' The only thing needed is the runPass$ switch ' and runExit$ = the name of the button to Click '   in the parent program ' - loginUserNum$    = trim$(word$(UserInfo$,1,"|"))      ' I always pass the user number who is logged in and running loginUserName$   = trim$(word$(UserInfo$,2,"|"))      ' I always pass the User Name loginCanMaint$   = trim$(word$(UserInfo$,3,"|"))      ' Does the user have maintenance priv loginKeyKeeper$  = trim$(word$(UserInfo$,4,"|"))      ' Does the user have keyKeeper priv - they can do anything runProg$         = trim$(word$(UserInfo$,5,"|"))      ' Sometimes you need to know what the Parent program name is runProgData$      = trim$(word$(UserInfo$,6,"|"))      ' Any other data you may need runPass$         = trim$(word$(UserInfo$,7,"|"))      ' runPass$ = [Y]es means it was run from a Parent, else it's run directly from RunBasic runExit$         = trim$(word$(UserInfo$,8,"|"))      ' The ID of your [Exit] button in the parent if runExit$          = "" then runExit$ = "exitApp"  ' Set the default ID - remember this was set in the parent program loginUserNum     = val(loginUserNum$) if runPass$      <> "Y" then titlebar "Your Program Title" ' If this is a RUNNED program you cannot show a titlebar.

' - ' The Child exit button ' Somewhere in your program you need to have an [Exit] ' The Child uses a normal exit button. ' And sets the ID to "exitMe" ' If the runPass$ is [Y]es it knows to get the '  elementById of the [Exit] btton of the parent ' Now: ' When you click [Exit] if the parent ran it (runPass$ = "Y"), when the ' child clicks the parents [Exit] and it goes to where the normal [Exit] in the Parent goes. ' If it was run by RunBasic (runPass$ <> "Y") then it goes to your normal [doExit] tag in the Child here. ' - button   #exit, "Exit",[doExit]                ' the Child [Exit] button #exit setid("exitMe")                ' set the ID to "exitMe" if runPass$ = "Y" then                       ' if parent ran this find the element in the browser html "    document.getElementById('exitMe').name = document.getElementById('";runExit$;"').name;    " end if

[doExit] ' do whatever you want in this Child

code