The CheckBox class represents a check box control.
// define the checkbox sample form
class MyForm extends Form {
// the textbox the event handler writes to
textbox = null;
constructor(caption, x_pos, y_pos, width, height) {
// the constructor gets called when a new object is created;
// the super() function calls the constructor on the base
// class, which in this case is Form since MyForm extends Form
super(caption, x_pos, y_pos, width, height);
// create checkboxes with various color labels
const checkbox1 = new CheckBox("Red");
const checkbox2 = new CheckBox("Yellow");
const checkbox3 = new CheckBox("Green");
const checkbox4 = new CheckBox("Blue");
// create a new line
const line = new Line(Line.Horizontal);
// create a new textbox; store it on this.textbox so the
// event handler can output results to it
this.textbox = new TextBox();
this.textbox.setMultiline(true);
// add the checkboxes to the top part of the form;
// use a layout object to space the elements horizontally
const layout_top = new BoxLayout(Layout.Horizontal);
layout_top.addSpacer();
layout_top.add(checkbox1, 0, Layout.Expand | Layout.Top | Layout.Bottom, 5);
layout_top.addSpacer(10);
layout_top.add(checkbox2, 0, Layout.Expand | Layout.Top | Layout.Bottom, 5);
layout_top.addSpacer(10);
layout_top.add(checkbox3, 0, Layout.Expand | Layout.Top | Layout.Bottom, 5);
layout_top.addSpacer(10);
layout_top.add(checkbox4, 0, Layout.Expand | Layout.Top | Layout.Bottom, 5);
// add the checkboxes, line, and the textbox to the form
const layout_main = new BoxLayout(Layout.Vertical);
layout_main.addSpacer();
layout_main.add(layout_top, 0, Layout.Expand | Layout.Left | Layout.Right, 5);
layout_main.addSpacer(10);
layout_main.add(line, 0, Layout.Expand | Layout.Left | Layout.Right, 5);
layout_main.addSpacer(10);
layout_main.add(this.textbox, 1, Layout.Expand | Layout.Left | Layout.Right, 5);
layout_main.addSpacer();
// use layout_main for the main form layout
this.setLayout(layout_main);
// connect the checkbox events to the event handler
checkbox1.click.connect(this, this.onCheckBoxClicked);
checkbox2.click.connect(this, this.onCheckBoxClicked);
checkbox3.click.connect(this, this.onCheckBoxClicked);
checkbox4.click.connect(this, this.onCheckBoxClicked);
}
onCheckBoxClicked(sender, event_args) {
// when a checkbox is clicked, output the label and
// check status to the textbox
const state = sender.getValue() ? "checked" : "unchecked";
this.textbox.appendText(`${sender.getLabel()} ${state}.\n`);
}
}
// create a new checkbox sample form
const f = new MyForm("CheckBox Example", 0, 0, 400, 300);
f.center();
f.show();
Application.run(); Returns the text associated with the check box control.
Returns the text associated with the check box control.
Returns true if the check in the check box control is visible, and false otherwise.
Returns true if the check in the check box control is visible. Returns false if the check in the check box control is not visible.
Sets the text associated with the check box control.
Turns the check in the check box on if flag is true. Turns the check in the check box off if flag is false.