Page 1 of 1

Touch List Box

Posted: Fri Mar 29, 2019 9:27 am
by Claus Nielsen
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.

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;
		}
    }
}
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.

Re: Touch List Box

Posted: Mon Apr 29, 2019 4:12 pm
by Russ C.
I've returned the selected element in the past using this code:

Code: Select all

	void GetSelectedValue(Controls.TouchComboBox.TouchComboBoxHostCF touchComboBox)
		{
			MessageBox.Show(touchComboBox.SelectedItem.ToString());
		}

Re: Touch List Box

Posted: Sun May 19, 2019 1:54 pm
by Claus Nielsen
Tried this, and just again. The selected item is null.

Touch List Box

Posted: Tue Sep 03, 2019 4:46 am
by LenDamedy
Touch Combo Box ı kullanacağım. Nasıl kullanacağımı bilmiyorum. Toplam 8 ihtimal olacak ve hepsini bir integer değere atamak istiyorum. Mesela ;

Vana1 seçildiğinde 1 değeri atayacak.
Vana2 seçildiğinde 2 değeri atayacak.
Vana3 seçildiğinde 3 değeri atayacak.
.
.
.
Vana8 seçildiğinde 8değeri atayacak.

Nasıl yapabiliriz ?

If you want to read more information regarding What Swedes call their second largest city or Can letting agencies charge for references check out our website