Small Basic: How to Make a Check Box
Small Basic has following three kinds of controls.
- button
- multi-line text box
- text box
But no check box is available in Controls object.
So, let's make a check box by ourself.
Design
To make a check box, we need a box, a check mark, and mouse event handler. A box can be drawn with GraphicsWindow.FillRectangle(). A check mark can be drawn with Shapes.AddText() and "Wingdings 2" font. In mouse event handler, let's check whether mouse is clicked in the box or not and change the state of the check box.
Code
This is a sample code for a check box. The first one is a mouse event handler.
Sub OnMouseDown
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
For iCheckBox = 1 To cboxNum
x0 = cboxObj[iCheckBox]["x0"]
x1 = cboxObj[iCheckBox]["x1"]
y0 = cboxObj[iCheckBox]["y0"]
y1 = cboxObj[iCheckBox]["y1"]
If x0 <= mx And mx <= x1 And y0 <= my And my <= y1 Then
If cboxObj[iCheckBox]["checked"] Then
Shapes.HideShape(cboxObj[iCheckBox]["checkmark"])
cboxObj[iCheckBox]["checked"] = "false"
Else
Shapes.ShowShape(cboxObj[iCheckBox]["checkmark"])
cboxObj[iCheckBox]["checked"] = "true"
EndIf
EndIf
EndFor
EndSub
The second one is a subroutine to check a check box.
Sub IsChecked
iCheckBox = Text.GetSubTextToEnd(param["checkBoxName"], 9)
return = cboxObj[iCheckBox]["checked"]
EndSub
The third one is to add a check box.
Sub AddCheckBox
' param["left"] - the x co-ordinate of the check box
' param["top"] - the y co-ordinate of the check box
' returns return - the check box that was just added to the Graphics Window.
cboxColor = GraphicsWindow.BrushColor
cboxSize = GraphicsWindow.FontSize
cboxFont = GraphicsWindow.FontName
cboxNum = cboxNum + 1
cboxX0 = param["left"]
cboxX1 = cboxX0 + cboxSize + 1
cboxY0 = param["top"] + 1
cboxY1 = cboxY0 + cboxSize + 1
GraphicsWindow.BrushColor = "Gray"
GraphicsWindow.FillRectangle(cboxX0, cboxY0, cboxSize + 2, cboxSize + 2)
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillRectangle(cboxX0 + 1, cboxY0 + 1, cboxSize, cboxSize)
cboxObj[cboxNum]["x0"] = cboxX0
cboxObj[cboxNum]["x1"] = cboxX1
cboxObj[cboxNum]["y0"] = cboxY0
cboxObj[cboxNum]["y1"] = cboxY1
GraphicsWindow.BrushColor = cboxColor
GraphicsWindow.FontName = "Wingdings 2"
cboxMark = Shapes.AddText("P")
Shapes.Move(cboxMark, param["left"] + 1, param["top"] + 3)
Shapes.HideShape(cboxMark)
cboxObj[cboxNum]["checkmark"] = cboxMark
cboxObj[cboxNum]["checked"] = "False"
GraphicsWindow.FontName = cboxFont
cboxMark = Shapes.AddText(param["caption"])
Shapes.Move(cboxMark, param["left"] + cboxSize * 1.5 , param["top"])
return = "CheckBox" + cboxNum
EndSub
Sample Program
I uploaded a sample program as TCX734. This program can generate a PIN code. Including alphabet or not can be select with our check box.