Maddy123
Topic Author
Posts: 11
Joined: 23 Oct 2013, 23:19

Creating textbox dynamically.

16 Jan 2014, 00:16

I am trying to create textbox dynamically and also keep updating its values. Is there any way to add textbox to root ? Thanks.
 
User avatar
sfernandez
Site Admin
Posts: 1920
Joined: 22 Dec 2011, 19:20

Re: Creating textbox dynamically.

16 Jan 2014, 10:37

Hi,

All UI elements that can be defined in the XAML can also be created by code.

If you are working with the Unity's C# API, you have to do the following to dynamically create a TextBox and add it to the root:
using System;
using Noesis;

// Attached next to the NoesisGUIPanel component
public class Test: MonoBehavior
{
    void Start()
    {
        // create TextBox
        var tb = new TextBox();
        // you can specify an initial text
        tb.SetText("Type here...");
        // and some layout properties
        tb.SetHorizontalAlignment(HorizontalAlignment.Center);
        tb.SetVerticalAlignment(VerticalAlignment.Center);
        tb.SetWidth(200.0f);

        // get root (supposing it is a Grid element)
        var gui = GetComponent<NoesisGUIPanel>();
        var root = gui.GetRoot<Grid>();

        // add TextBox to the root
        root.GetChildren().Add(tb);
    }
}
 
Maddy123
Topic Author
Posts: 11
Joined: 23 Oct 2013, 23:19

Re: Creating textbox dynamically.

16 Jan 2014, 18:23

Actually, I'm using C++. Root doesn't appear to have a getChildren() method.
 
User avatar
sfernandez
Site Admin
Posts: 1920
Joined: 22 Dec 2011, 19:20

Re: Creating textbox dynamically.

16 Jan 2014, 18:42

Could you please attach/paste here the xaml (or part of the xaml) you are using?
The code for adding the TextBox will vary depending on the container used at the root.

Assuming the root is a Grid panel, the code in C++ will be like this:
#include <NsGui/TextBox.h>
#include <NsGui/Grid.h>
#include <NsGui/UIElementCollection.h>

using Noesis;
using Noesis::Core;
using Noesis::Gui;

void AddTextBox(FrameworkElement* root)
{
    // create TextBox
    Ptr<TextBox> tb = *new TextBox();
    // you can specify an initial text
    tb->SetText("Type here...");
    // and some layout properties
    tb->SetHorizontalAlignment(HorizontalAlignment_Center);
    tb->SetVerticalAlignment(VerticalAlignment_Center);
    tb->SetWidth(200.0f);

    // get root (supposing it is a Grid element)
    Grid* grid = NsDynamicCast<Grid*>(root);

    // add TextBox to the root
    grid->GetChildren()->Add(tb.GetPtr());
}

Who is online

Users browsing this forum: No registered users and 2 guests