Creating textbox dynamically.
I am trying to create textbox dynamically and also keep updating its values. Is there any way to add textbox to root ? Thanks.
-
sfernandez
Site Admin
- Posts: 3184
- Joined:
Re: Creating textbox dynamically.
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:
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:
Code: Select all
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);
}
}
Re: Creating textbox dynamically.
Actually, I'm using C++. Root doesn't appear to have a getChildren() method.
-
sfernandez
Site Admin
- Posts: 3184
- Joined:
Re: Creating textbox dynamically.
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:
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:
Code: Select all
#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: Ahrefs [Bot] and 3 guests