Get value of input in dropdown (C#)
NicklasOestergaard opened this issue · 3 comments
NicklasOestergaard commented
I have created a dropdown that has inputs. Have do i "get" the cleand value of the inputs?
riteshchandawar commented
@ke-yu Can you please help him to resolve his issue.
Thanks,
Ritesh
NicklasOestergaard commented
My current code looks like this:
using System.Collections.Generic;
using System.Linq;
using DSCoreNodesUI;
using Dynamo.Nodes;
using Dynamo.Utilities;
using ProtoCore.AST.AssociativeAST;
using Dynamo.Models;
using System;
using Dynamo.Engine;
namespace BIMSharkDynamoTools.GETCalls
{
[NodeName("GET All Your BS Projects")]
[NodeDescription("GET a list of all your BS projects")]
[IsDesignScriptCompatible]
public class BIMSharkDynamoToolsGETProjects : DSDropDownBase
{
private RestService.Rest rest;
private string apiKey = "";
//public static string apiKey;
internal EngineController EngineController { get; set; }
//test
//public BIMSharkDynamoToolsGETProjects()
// : base("item")
//{
// this.AddPort(PortType.Input, new PortData("BS ApiKey", "ToolTip"), 0);
// this.PropertyChanged += OnPropertyChanged;
//}
public BIMSharkDynamoToolsGETProjects()
: base("Project(s)")
{
InPortData.Add(new PortData("BS ApiKey", "ToolTip"));
RegisterAllPorts();
this.PropertyChanged += OnPropertyChanged;
}
void OnPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName != "CachedValue")
return;
if (InPorts.Any(x => x.Connectors.Count == 0))
return;
PopulateItems();
}
public override void PopulateItems()
{
try
{
List<string> BSAPIKeys = new List<string>();
string AllBSAPIKeys = "";
BSAPIKeys.Clear();
var urls = InputNodes.Values.ToArray();
foreach (var url in urls)
{
if (InPorts.Any(x => x.PortName == "BS ApiKey"))
{
BSAPIKeys.Add(((Dynamo.Nodes.CodeBlockNodeModel)url.Item2).Code);
}
}
AllBSAPIKeys = string.Join(", ", BSAPIKeys.ToArray());
apiKey = AllBSAPIKeys.Replace(";", "").Replace("/", "").Replace("\"", "");
Items.Clear();
}
catch
{
}
if (apiKey.Length > 20)
{
try
{
// The Items collection contains the elements
// that appear in the list. For this example, we
// clear the list before adding new items, but you
// can also use the PopulateItems method to add items
// to the list.
rest = new RestService.Rest(apiKey);
//Items.Clear();
// Create a number of DynamoDropDownItem objects
// to store the items that we want to appear in our list.
rest.GetProjects();
var newItems = new List<DynamoDropDownItem>();
foreach (RestService.Project p in rest.projects)
{
string text = "(" + p.id.ToString() + ") " + p.project_name.ToString();
string value = p.id.ToString();
//int value = int.Parse(p.id);
//CB_Projects.Items.Add("(" + p.id + ") " + p.project_name);
//new DynamoDropDownItem("(" + d.id + ") " + d.title, d.id)),
Items.Add(new DynamoDropDownItem(text, value));
}
//set up the collection
//var newItems = new List<DynamoDropDownItem>();
//set up the collection
//Items.AddRange(newItems);
// Set the selected index to something other
// than -1, the default, so that your list
// has a pre-selection.
SelectedIndex = 0;
return;
}
catch
{
}
}
else
{
try
{
// The Items collection contains the elements
// that appear in the list. For this example, we
// clear the list before adding new items, but you
// can also use the PopulateItems method to add items
// to the list.
Items.Clear();
// Create a number of DynamoDropDownItem objects
// to store the items that we want to appear in our list.
var newItems = new List<DynamoDropDownItem>()
{
new DynamoDropDownItem("No Project found", 0),
};
//set up the collection
//var newItems = new List<DynamoDropDownItem>();
//set up the collection
//Items.AddRange(newItems);
// Set the selected index to something other
// than -1, the default, so that your list
// has a pre-selection.
SelectedIndex = 0;
}
catch
{
}
}
}
public override IEnumerable<AssociativeNode> BuildOutputAst(List<AssociativeNode> inputAstNodes)
{
//var intNode = AstFactory.BuildIntNode((int)Items[SelectedIndex].Item);
//var assign = AstFactory.BuildAssignment(GetAstIdentifierForOutputIndex(0), intNode);
//return new List<AssociativeNode> { assign };
// If there the dropdown is still empty try to populate it again
if (Items.Count == 0 || Items.Count == -1)
{
PopulateItems();
}
// get the selected items name
var stringNode = AstFactory.BuildStringNode((string)Items[SelectedIndex].Item);
// assign the selected name to an actual enumeration value
var assign = AstFactory.BuildAssignment(GetAstIdentifierForOutputIndex(0), stringNode);
// return the enumeration value
return new List<AssociativeNode> { assign };
}
}
}
sharadkjaiswal commented
@NicklasOestergaard Please note that we have changed PopulateItems() API and it's no longer a virtual method, you may need to override PopulateItemsCore() as mentioned here.