diesel76
Topic Author
Posts: 2
Joined: 20 Feb 2015, 20:22

combobox selected item

20 Feb 2015, 20:28

I try to get the selected item from a combobox dynamically created without any success

here is the code
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);
  }
}
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:
String val = mybox.GetSelectedItem ().AsString();
Debug.Log (val);
 
User avatar
sfernandez
Site Admin
Posts: 1911
Joined: 22 Dec 2011, 19:20

Re: combobox selected item

23 Feb 2015, 17:08

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:
public void changed(BaseComponent sender, Noesis.RoutedEventArgs args)
{
  ComboBox cb = sender.As<ComboBox>();
  if (cb != null)
  {
    string selected = cb.GetSelectedItem().AsString();
    Debug.log(selected);
  }
}
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:
public void changed(object sender, RoutedEventArgs args)
{
  ComboBox cb = sender as ComboBox;
  if (cb != null)
  {
    string selected = (string)cb.GetSelectedItem();
    Debug.log(selected);
  }
}
Easier, don't you think ;)

Who is online

Users browsing this forum: No registered users and 1 guest