What is a control?

What is a control and how they interact with standard core components

TVision2 Core offers a game loop application model: at each cycle all components have the chance to update and redraw themselves. Each component receives a list of all events produced during that cycle, and the component can handle all of some if it wants.

This model works great on games, but is not how we usually think when we build an UI based application. Almost all UI frameworks are “event-based”: you create some sort of element, let’s say a button, and then attach some code when some events happen on this button. If user clicks the button (the event) something happens (the attached code, aka the handler).

TvControls offers a set of controls that allow you to use this event-based model approach for building your application. If using TvControls you can forget about the “game loop” at all. Of course, it is still running, but your code have to react only to the events generated by the controls you create.

Controls versus components

TvControls is built on top of TVision2 Core and do not replaces it. You don’t have to choose between using TvControls or TVision2 Core. You can use both of them simultaniously if you want. A key concept of TvControls is that every control should behave like a standard component in all the contexts where a component is expected.

Controls are internally implemented using TVision2 Core components, but this components are wrapped inside a control class which provides all extra functionality that is needed for the event-model approach to work.

You can “convert” every control to a standard component using the AsComponent() method that all controls provide.

The TvControl<T> class

The TvControl<T> class is the base class for all controls. All controls inherit from TvControl. This class wraps an instance of TvComponent<T> which is the underlying TVision2 Core component that implements the control. The class TvControl<T> implements the ITvControl interface.

You can add a control exactly in the same way that do you can add a component, that is through the method Add of the UI property of the ITuiEngine object:

tui.UI.Add(myControl);    // myControl is an instance of any control class

This code compiles because TvControls offers an extension method over the IComponentTree to accept any ITvControl instance. However this method is only provided for easy of use. Do you remember that you can use a control in every context that a standard core component is used? So, if you want you can use the following code to add your control, and it will still work (and behave) like a control:

tui.UI.Add(myControl.AsComponent());

Last modified May 18, 2020: more docs (4830683)