2021-05-03 17:32:57 +02:00
# Checkbox (lv_checkbox)
2021-04-12 18:19:04 +02:00
## Overview
2021-08-26 10:52:39 +02:00
The Checkbox object is created from a "tick box" and a label. When the Checkbox is clicked the tick box is toggled.
2021-04-12 18:19:04 +02:00
## Parts and Styles
2022-03-21 18:25:51 +08:00
- `LV_PART_MAIN` The is the background of the Checkbox and it uses the text and all the typical background style properties.
2021-05-03 17:32:57 +02:00
`pad_column` adjusts the spacing between the tickbox and the label
2022-03-21 18:25:51 +08:00
- `LV_PART_INDICATOR` The "tick box" is a square that uses all the typical background style properties.
2021-08-26 10:52:39 +02:00
By default, its size is equal to the height of the main part's font. Padding properties make the tick box larger in the respective directions.
2021-04-12 18:19:04 +02:00
2022-03-21 18:25:51 +08:00
The Checkbox is added to the default group (if it is set).
2021-04-12 18:19:04 +02:00
## Usage
### Text
2021-06-09 15:10:35 +02:00
The text can be modified with the `lv_checkbox_set_text(cb, "New text")` function and will be dynamically allocated.
2021-05-03 17:32:57 +02:00
2022-03-21 18:25:51 +08:00
To set a static text,
2021-06-09 15:10:35 +02:00
use `lv_checkbox_set_static_text(cb, txt)` . This way, only a pointer to `txt` will be stored. The text then shouldn't be deallocated while the checkbox exists.
2021-05-03 17:32:57 +02:00
### Check, uncheck, disable
2021-06-09 15:10:35 +02:00
You can manually check, un-check, and disable the Checkbox by using the common state add/clear function:
2021-05-03 17:32:57 +02:00
```c
lv_obj_add_state(cb, LV_STATE_CHECKED); /*Make the chekbox checked*/
lv_obj_clear_state(cb, LV_STATE_CHECKED); /*MAke the checkbox unchecked*/
lv_obj_add_state(cb, LV_STATE_CHECKED | LV_STATE_DISABLED); /*Make the checkbox checked and disabled*/
```
2021-04-12 18:19:04 +02:00
2022-07-07 22:34:04 +02:00
To get whether the checkbox is checked or not use: `lv_obj_has_state(cb, LV_STATE_CHECKED)` .
2022-07-07 20:41:49 +02:00
2021-04-12 18:19:04 +02:00
## Events
2021-05-03 17:32:57 +02:00
- `LV_EVENT_VALUE_CHANGED` Sent when the checkbox is toggled.
2021-07-07 16:18:56 +02:00
- `LV_EVENT_DRAW_PART_BEGIN` and `LV_EVENT_DRAW_PART_END` are sent for the following types:
- `LV_CHECKBOX_DRAW_PART_BOX` The tickbox of the checkbox
2022-03-21 18:25:51 +08:00
- `part` : `LV_PART_INDICATOR`
2021-07-07 16:18:56 +02:00
- `draw_area` : the area of the tickbox
- `rect_dsc`
See the events of the [Base object ](/widgets/obj ) too.
2021-04-12 18:19:04 +02:00
Learn more about [Events ](/overview/event ).
## Keys
The following *Keys* are processed by the 'Buttons':
2021-05-03 17:32:57 +02:00
- `LV_KEY_RIGHT/UP` Go to toggled state if toggling is enabled
- `LV_KEY_LEFT/DOWN` Go to non-toggled state if toggling is enabled
- `LV_KEY_ENTER` Clicks the checkbox and toggles it
2021-04-12 18:19:04 +02:00
Note that, as usual, the state of `LV_KEY_ENTER` is translated to `LV_EVENT_PRESSED/PRESSING/RELEASED` etc.
Learn more about [Keys ](/overview/indev ).
## Example
```eval_rst
2022-08-15 09:27:00 -04:00
.. include:: ../../examples/widgets/checkbox/index.rst
2021-04-12 18:19:04 +02:00
```
## API
```eval_rst
.. doxygenfile:: lv_checkbox.h
:project: lvgl
```