User Tools

Site Tools


Creating child controls in a loop

by Richard Russell, March 2013

In Liberty BASIC 4.04 you can use the MAPHANDLE statement to, for example, reuse the same code to open multiple windows. The LB4 documentation illustrates how to open three windows using this technique:

    winName$ = "first second third"
    for x = 1 to 3
      call createWindow word$(winName$, x)
    next x
  sub createWindow title$
    texteditor #1.te, 10, 10, 200, 200
    open "text "+title$ for window as #1
    #1.te "this is the "+title$+" window"
    #1 "trapclose aboutToClose"
    handle$ = "#"+title$
    maphandle #1, handle$
  end sub
  sub aboutToClose handle$
    confirm "Close "+handle$+"?"; answer
    if answer = 1 then close #handle$
  end sub

It would be extremely useful to be able to use MAPHANDLE in a similar fashion to create multiple child controls, but for some reason LB4 will not let you do so. Fortunately this is one of the many limitations that is removed in LB Booster.

The code below is adapted from the Alphabet Buttons example on the Liberty BASIC Community Wiki but instead of using a separate BUTTON statement for each button, all 26 are created in a loop:

      Stylebits #win, 0,_WS_MAXIMIZEBOX,0,0
      WindowWidth = 230
      WindowHeight = 80
      for i = 1 to 26
        x = 15 * i : y = 5
        if x > 195 then x = x - 195 : y = y + 15
        Button #win.tmp, CHR$(64+i), sel, UL, x, y,  15,  15
        maphandle #win.tmp, "#win." + chr$(64+i)
      Open "Alphabet Buttons" For Window As #win
      Print #win, "Trapclose quit"
  sub sel handle$
      print "Selected "; handle$
  end sub
  sub quit handle$
      close #handle$
  end sub

If you click a button and watch the mainwin you can see that each button has a separate handle, exactly as was the case in the original program.

creating_20child_20controls_20in_20a_20loop.txt · Last modified: 2018/04/17 15:25 by tbest3112