Example program - Resizable forms and anchoring
This example program shows the use of anchors to allow a form to be resized at runtime. The main edit control and the static text are anchored on both sides so they adjust their width as the form gets bigger or smaller.
Similarly the buttons at the bottom are anchored to the bottom so they stay a fixed distance from that edge. The outer OK and On/Off buttons are also anchored to the left and right edges so they stay in the same balanced position as the form widens. However the middle cancel button cannot do that. If anchored to both edges it would be widened to keep the edges in the same place. The solution to this problem is to add a Resize() event handler and center the cancel button manually. The on/off button toggles on and off the actioning of this event so you can see its effect.
Note that for a form to be resizable the design time Resize property must be set in the forms designer. The forms designer has a special anchor mode to set anchors.
- DEFFORM Form1()={.form,.form$,.Style=0x50c400c4,.Width=233,.Height=142,.text$="Anchoring demonstration",.Id=1024},\ {.ok,.button$,.Style=0x50010001,.Left=13,.Top=120,.Width=50,.Height=14,.text$="OK",.__Anchor=10,.Id=1},\ {.cancel,.button$,.Style=0x50010000,.Left=87,.Top=120,.Width=50,.Height=14,.text$="Cancel",.__Anchor=8,.Id=2},\ {.btnResize,.button$,.Style=0x50010000,.Left=161,.Top=120,.Width=50,.Height=14,.text$="&On",.__Anchor=12,.Id=1002,.Type=9,.Click()},\ {.editControl1,.kcmldbedit$,.Style=0x508100a0,.Left=11,.Top=18,.Width=205,.Height=47,.__Anchor=15,.Id=1000},\ {.textControl1,.static$,.Style=0x50000000,.Left=12,.Top=73,.Width=205,.Height=40,.text$="This text, the multiline edit and the two outer buttons are positioned automatically by the anchoring mechanism. Alas the cancel button cannot be centered that way. If the resize event handler is on (see button in bottom right) then the cancel button will be centered by the event handler.",.__Anchor=14,.Id=1001} DIM resize_flag=TRUE + DEFEVENT Form1.form.Resize() REM resize event handler which can center the cancel button IF (resize_flag) .cancel.Left = .form.Left + (.form.Width - .cancel.Width) / 2 END IF END EVENT + DEFEVENT Form1.btnResize.Click() REM toggle event handler IF (resize_flag) ..text$ = "&Off" resize_flag = FALSE ELSE ..text$ = "&On" resize_flag = TRUE END IF END EVENT FORM END Form1 Form1.Open()