Native Windows GUI: Dynamic control

Controls can be instanced and added to a GUI at any time. This section explains how to create new control at runtime and add them to a window.

Dynamic creation

Adding controls at runtime is not something special. It's just the matter of creating a new control from a builder and using an existing window as its parent.
fn add_button(button: &mut nwg::Button, window: &nwg::Window) {
    nwg::Button::builder()
        .text("Hello")
        .flags(nwg::ButtonFlags::VISIBLE)
        .parent(window)
        .build(button);
}

Data management

There is no preferred way to manage the reference to dynamically created controls. Just use what's best for your application. For example, storing buttons into a RefCell<Vec<nwg::Button>> is completely fine.

Dynamic freeing

Controls resources are always freed at drop time. If you don't store the control somewhere, it will be freed just after being created.

Example