Touch List Box
Posted: Fri Mar 29, 2019 9:27 am
Been mocking around with the Touch List Box HMI control all day now, trying to populate it from script and get the currently selected item back. After a big dive into the classes these two helpers is what I came up with.
Is this really the only / most efficiant way to use these in scripting?Notice that the method to get the current item is using reflection as this was the only way I could come up with to get the item currently selected.
Code: Select all
namespace Neo.ApplicationFramework.Generated
{
using System.Windows.Forms;
using System;
using System.Drawing;
using Neo.ApplicationFramework.Tools;
using Neo.ApplicationFramework.Common.Graphics.Logic;
using Neo.ApplicationFramework.Controls;
using Neo.ApplicationFramework.Interfaces;
using System.Linq;
using System.Collections.Generic;
using Neo.ApplicationFramework.Common.Dynamics;
using System.Reflection;
public partial class Component
{
/// <summary>
/// Populates a Touch List Box HMI control with the items from the provided enumerable
/// </summary>
/// <param name="listbox">The list box to populate. Only the Touch version.</param>
/// <param name="item">The items to populate the listbox with.</param>
/// <returns>The binding list used to populate the list box. This object supports operations such as sort.</returns>
public StringIntervalListCF PopulateTouchListBox(Controls.Script.TouchListBoxAdapter listbox, IEnumerable<string> items)
{
// The HMI listbox is just an adapter. We need the adapted object to populate it.
var lb = listbox.AdaptedObject as Controls.TouchListBox.TouchListBoxHost;
// Instantiate the list that will be used to populate the list box with.
var lst = new StringIntervalListCF();
// Itterate all the items in the provided enumerable.
foreach (var row in items.Select((item, i) => new { item, i }))
{
lst.Add(new BindableStringIntervalCF
{
Start = row.i,
End = row.i,
Value = row.item
});
}
// Now set the list box datasource to the generated binding list.
lb.DataSource = lst;
lb.Texts = new System.ComponentModel.BindingList<string>(lst.Select(b => b.Value).ToList());
// Return the list for further operations, such as sort.
return lst;
}
/// <summary>
/// Get the currently selected item from a Touch List Box HMI control.
/// </summary>
/// <param name="listbox">The listbox to get the current item from. Only touch versions.</param>
/// <returns>The currently selected item.</returns>
public string GetSelectedValue(Controls.Script.TouchListBoxAdapter listbox)
{
// Get the property info for the hosted controls.
var hostedControlsPi = listbox.RescoObject.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.Where(p => p.Name == "HostedControl");
// Itterate all the properties found named 'HostedControl'.
foreach (var c in hostedControlsPi)
{
// Get the value of the current control and cast it to the IValueSelector type.
var hostedControl = c.GetValue(listbox.RescoObject) as IValueSelector;
// Check that the cast was successfull and return the selected item value.
if (hostedControl != null)
return ((BindableStringIntervalCF)hostedControl.SelectedItem).Value;
}
return null;
}
}
}