> For the complete documentation index, see [llms.txt](https://docs.flatredball.com/gum/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flatredball.com/gum/code/about/gum-in-code.md).

# Gum in Code

## Introduction

Gum can be used purely in code (no editor), or by referencing a Gum project. Both approaches are well-supported so you can use Gum to fit your development style.

## Gum Code-Only

Gum can be used fully in code. A code-only setup requires minimal code. The following shows a simple MonoGame Game1 class with a few functional Gum controls. All lines of code related to Gum are highlighted:

<pre class="language-csharp"><code class="lang-csharp"><strong>using Gum.Forms.Controls;
</strong><strong>using Gum.Wireframe;
</strong>using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
<strong>using Gum;
</strong>using System;

namespace MonoGameAndGum;

public class Game1 : Game
{
    private GraphicsDeviceManager _graphics;
<strong>    GumService GumUI => GumService.Default;
</strong>    public Game1()
    {
        _graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        IsMouseVisible = true;
    }

    protected override void Initialize()
    {
<strong>        GumUI.Initialize(this);
</strong>
<strong>        var stackPanel = new StackPanel();
</strong><strong>        stackPanel.AddToRoot();
</strong><strong>        stackPanel.Spacing = 6;
</strong><strong>        stackPanel.Anchor(Anchor.Center);
</strong>
<strong>        var button = new Button();
</strong><strong>        stackPanel.AddChild(button);
</strong><strong>        button.Text = "Click Me";
</strong><strong>        button.Click += (s, e) => 
</strong><strong>        {
</strong><strong>            button.Text = DateTime.Now.ToString();
</strong><strong>        };
</strong>
<strong>        var textBox = new TextBox();
</strong><strong>        textBox.Width = 150;
</strong><strong>        stackPanel.AddChild(textBox);
</strong>
<strong>        var listBox = new ListBox();
</strong><strong>        stackPanel.AddChild(listBox);
</strong><strong>        for(int i = 0; i &#x3C; 10; i++)
</strong><strong>        {
</strong><strong>            listBox.Items.Add("Item " + i);
</strong><strong>        }
</strong>
        base.Initialize();
    }


    protected override void Update(GameTime gameTime)
    {
<strong>        GumUI.Update(gameTime);
</strong>        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
<strong>        GumUI.Draw();
</strong>        base.Draw(gameTime);
    }
}


</code></pre>

[Try on XnaFiddle.NET](https://xnafiddle.net/#snippet=H4sIAAAAAAAAA3VQwU4CMRD9laanJSHNgtEDhIOsxpCoMbKJB9dD3R1kwm5r2lkhEv7ddltAiexh2zfvzUzf2_KZvWsbPiLTQp-3FtWH5aNX7oriBQ0sjGyAv_U5KiSUNX4DH_EvaZglWa6epIKaTZiCNZsfCklvXKgjL66rKtfPWtMpMf-UpdvoBlyddKhyqU0SDpGBIjC-1y9-b4m0ikunHfhnYbbEukqC1rPhJnLYkGst2jQdDrMayxV7gICOolDvqtMJS2yfQY9NPL64LdS2UMx9fwfeSIIcGxCPei1yPSfjfHXP2sVnkxNO9Sa-Ow-oU0TG5V3R0vGDy_SMnajcJ1Gj_TXyPqCzWUS1pxcuW1TE0LWmY3d03jI28CD4Dv_ewW7sFjOCxvqhSUjN4xhnTIxhZ5vvfgAhBmriXQIAAA)

<figure><img src="/files/nLmiSTPJiMeNwEwCCdrV" alt=""><figcaption><p>Code-only Gum Controls</p></figcaption></figure>

## Gum Tool Projects in Code

The Gum Tool can be used to create projects visually. These projects can be loaded into your project and accessed in a type-safe way using Gum's generated code.

The following image shows a screen similar to the one above created in the Gum tool:

<figure><img src="/files/I8Y1yTwHoIFUdol2o8l7" alt=""><figcaption></figcaption></figure>

The following code can be used to load and interact with the controls:

<pre class="language-csharp"><code class="lang-csharp">public class Game1 : Game
{
    private GraphicsDeviceManager _graphics;
<strong>    GumService GumUI => GumService.Default;
</strong>    public Game1()
    {
        _graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        IsMouseVisible = true;
    }

    protected override void Initialize()
    {
<strong>        GumUI.Initialize(this, "GumProject/GumProject.gumx");
</strong>
<strong>        var screen = new ExampleScreen();
</strong><strong>        screen.AddToRoot();
</strong>
<strong>        screen.ButtonStandardInstance.Click += (s,e) =>
</strong><strong>        {
</strong><strong>            screen.ButtonStandardInstance.Text = DateTime.Now.ToString();
</strong><strong>        };
</strong>
<strong>        for(int i = 0; i &#x3C; 10; i++)
</strong><strong>        {
</strong><strong>            screen.ListBoxInstance.Items.Add("Item " + (i + 1));
</strong><strong>        }
</strong>
        base.Initialize();
    }

    protected override void Update(GameTime gameTime)
    {
<strong>        GumUI.Update(gameTime);
</strong>        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
<strong>        GumUI.Draw();
</strong>        base.Draw(gameTime);
    }
}

</code></pre>

Generated code is optional - code can access Gum objects without any generated code as shown in the following block:

<pre class="language-csharp"><code class="lang-csharp">public class Game1 : Game
{
    private GraphicsDeviceManager _graphics;
<strong>    GumService GumUI => GumService.Default;
</strong>    public Game1()
    {
        _graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        IsMouseVisible = true;
    }

    protected override void Initialize()
    {
<strong>        GumUI.Initialize(this, "GumProject/GumProject.gumx");
</strong>
<strong>        var screen = ObjectFinder.Self.GumProjectSave.Screens
</strong><strong>            .First().ToGraphicalUiElement();
</strong><strong>        screen.AddToRoot();
</strong>
<strong>        var button = screen.GetFrameworkElementByName&#x3C;Button>("ButtonStandardInstance");
</strong><strong>        button.Click += (s,e) =>
</strong><strong>        {
</strong><strong>            button.Text = DateTime.Now.ToString();
</strong><strong>        };
</strong>
<strong>        var listBox = screen.GetFrameworkElementByName&#x3C;ListBox>("ListBoxInstance");
</strong><strong>        for (int i = 0; i &#x3C; 10; i++)
</strong><strong>        {
</strong><strong>            listBox.Items.Add("Item " + (i + 1));
</strong><strong>        }
</strong>
        base.Initialize();
    }


    protected override void Update(GameTime gameTime)
    {
<strong>        GumUI.Update(gameTime);
</strong>        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
<strong>        GumUI.Draw();
</strong>        base.Draw(gameTime);
    }
}
</code></pre>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.flatredball.com/gum/code/about/gum-in-code.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
