combobox selected item
I try to get the selected item from a combobox dynamically created without any success
here is the code
Any thing i try it sucks lol, do you have any suggestions ?
I have a combobox it's alright with his arguments but when i try to get the selected item i have a problem of conversion type or a nullreference it seams everytime empty
Thanks for all i found the solution:
here is the code
Code: Select all
using UnityEngine;
using System.Collections;
using Noesis;
using System;
public class combo : MonoBehaviour
{
private Grid _root;
public Collection collection;
public ComboBox mybox;
void Start ()
{
mybox = new ComboBox ();
collection = new Collection();
collection.Clear ();
collection.Add("moi");
collection.Add("toi");
_root = GetComponent<NoesisGUIPanel> ().GetRoot<Grid> ();
Canvas mycanvas = _root.FindName<Canvas>("TestCanvas");
mybox.SetItemsSource (collection);
mybox.SelectionChanged += this.changed;
mybox.SetWidth (200);
mycanvas.GetChildren().Add(mybox);
}
public void changed(BaseComponent c, Noesis.RoutedEventArgs args)
{
ComboBoxItem cbi = mybox.GetSelectedItem() as ComboBoxItem;
Debug.log(cbi as String);
}
}I have a combobox it's alright with his arguments but when i try to get the selected item i have a problem of conversion type or a nullreference it seams everytime empty
Thanks for all i found the solution:
Code: Select all
String val = mybox.GetSelectedItem ().AsString();
Debug.Log (val);-
-
sfernandez
Site Admin
- Posts: 3363
- Joined:
Re: combobox selected item
For the code you posted it looks you are using Uniy package 1.1.x version. The API of that version is not very intuitive because you cannot use c# as cast operator, but our explicit cast functions:
We changed this in our upcoming 1.2 version (you can already try it if you become a verified customer), so API behaves as expected by any C# user:
Easier, don't you think 
Code: Select all
public void changed(BaseComponent sender, Noesis.RoutedEventArgs args)
{
ComboBox cb = sender.As<ComboBox>();
if (cb != null)
{
string selected = cb.GetSelectedItem().AsString();
Debug.log(selected);
}
}Code: Select all
public void changed(object sender, RoutedEventArgs args)
{
ComboBox cb = sender as ComboBox;
if (cb != null)
{
string selected = (string)cb.GetSelectedItem();
Debug.log(selected);
}
}Who is online
Users browsing this forum: Bing [Bot] and 5 guests