Forms Controls
Introduction
Forms controls are a collection of classes which provide common UI behavior. You will probably be familiar with many controls. The most common controls include:
Button
CheckBox
ComboBox
Image
Label
ListBox
RadioButton
ScrollViewer
Slider
TextBox
All controls are in the MonoGameGum.Forms.Controls
namespace.
Common Control Properties
All controls share a few common properties and characteristics. The following list provides a high-level introduction to forms control similarities. If the list doesn't make sense don't worry, we'll cover each topic in this and following tutorials.
All controls can be added to a StackPanel. Technically, any control can be added to any other control, but for this tutorial we'll keep things simple by adding only to a StackPanel.
All controls have a Visual property which can be used to position, size, and perform advance layout behavior. This Visual property is ultimately a GraphicalUiElement which provides access to all Gum layout properties.
All controls can receive input from the mouse (usually through the Gum Cursor object). Most controls can also receive focus and input from gamepads.
All controls support binding by assigning their BindingContext property. Children controls inherit BindingContext from their parents if the BindingContext is not explicitly assigned.
For the rest of this tutorial we'll add a few of the most common controls to our project and show how to work with them. Note that for this tutorial I've removed the Button control from the previous tutorial.
We also assume that your project has a mainPanel
to hold all controls.
Forms Control vs Visual
As we'll see below, each forms control has a specific purpose. Buttons are clicked, Labels display read-only strings, and TextBoxes can be used to input text. Each control provides properties and events specific to its purpose, standardizing the way each works.
However, each control also wraps a Visual object which gives you full layout control. The Visual property is of type GraphicalUiElement, and it has access to the full Gum layout engine. For example, a button could be made to be as wide as its parents using the following code:
Code Organization
For the sake of brevity, we will add all of our controls to the Root object, and all code will be added to the game's Initialize method after the Root has been created. Of course a full game may require more advanced organization but we'll keep everything in the Game Initialize for simplicity.
Label
Labels are text objects which can display a string. Labels do not have any direct interaction such as responding to clicks. The following code adds a label to the project:
The rest of this tutorial assumes that the Label is not removed. It is used to show when events have occurred.
Button
Button controls are usually added when a user needs to perform a command. Buttons can be clicked with the mouse and gamepad, and their click event can be manually invoked for custom support such as pressing Enter on the keyboard.
The following code creates two buttons. One is disabled so it does not respond to click events:
CheckBox
CheckBox controls allow the user to toggle a bool value by clicking on it. Just like Button, the CheckBoxes support clicking with mouse and gamepad and changing the IsChecked property in code.
The following code creates two CheckBoxes:
ComboBox
ComboBox provides a collapsible way to display and select from a list of options.
The following code creates a ComboBox which raises an event whenever an item is selected.
ListBox
ListBox provides a way to display a list of items. Each item can be selected.
The following code creates a ListBox which raises an event whenever an item is selected.
RadioButton
RadioButton controls allow the user to view a set of options and pick from one of the available options. Radio buttons are mutually exclusive within their group. Radio buttons can be grouped together by putting them in common containers, such as StackLayouts.
The following creates six radio buttons in two separate groups.
ScrollViewer
ScrollViewer provides a scrollable panel for controls. ScrollViewers are similar in concept to ListBoxes, but they can contain any type of item rather than only ListBoxItems.
The following code creates a ScrollViewer and adds buttons using AddChild.
Slider
Slider controls allow the user to select a value between a minimum and maximum value.
The following code creates a Slider which raises an event whenever its Value changes.
TextBox
TextBox controls allow the user to see and edit string values. TextBoxes support typing with the keyboard, copy/paste, selection, and multiple lines of text.
TextBoxes are automatically focused when clicked, but IsFocused can be explicitly set to give focus.
The following code creates a TextBox which raises an event whenever its text is changed. The text is then copied over to a label.
Last updated
Was this helpful?