Loop through onscreen controls

Locked
mark.monroe
Posts: 824
Joined: Tue Mar 13, 2012 9:53 am

Loop through onscreen controls

Post by mark.monroe »

You may wish to dynamically loop through a set of controls on a screen. To do that you need a function that you can give the name of a Text control to and get back a pointer to that control. In this example I create a function that allows me to pass in a Text control name and get back a reference to that control. The Text control in my example is called "TQTERM_A7" and the project name is "TextControlLoop", and the screen I want to do it in is called "FSystemInfo".

First thing you need to do is open up the screen's cs file that you want to loop through the controls on. Screen cs files are only created after you build your project. They are located in "TextControlLoop\BuildFiles", where "TextControlLoop" is the name of the project you are working on.
Snap 2013-09-20 at 12.06.16.jpg
Snap 2013-09-20 at 12.06.16.jpg (68.47 KiB) Viewed 45013 times
The iX Text control is stored as a Neo.ApplicationFramework.Controls.Controls.Label in "FSystemInfo.cs". Normally I would use the control like, "this.TQTERM_A7.Text = "New Text";". As we can see in the cs file, TQTERM_A7 is function that simply castes "m_TQTERM_A7" into a TextControlCFAdapter.

Here is the code:

Code: Select all

//--------------------------------------------------------------
// Press F1 to get help about using script.
// To access an object that is not located in the current class, start the call with Globals.
// When using events and timers be cautious not to generate memoryleaks,
// please see the help for more information.
//---------------------------------------------------------------

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.Reflection;
	using Neo.ApplicationFramework.Controls.Script;
	
	public partial class FSystemInfo
	{
		
		void Button1_Click(System.Object sender, System.EventArgs e)
		{
			//If you look at the FSystemInfo.cs file that is generated, you will see a field called m_TQTERM_A7.
			//That field is used to hold the Text control on our screen. It is of type Label in the case of 
			//a Text control.
			TextControlCFAdapter myTxt = GetTextBox("m_TQTERM_A7");
			//Now we can use the text control just like we can if we do 'this.TQTERM_A7.Text = "New Text"'
			myTxt.Text = "New Text";
		}
		
		//This function takes a string that repersents the name of an iX Text control that is part of this screen.
		//The field names can be found by looking at this screen's cs file found in the project folder.
		public TextControlCFAdapter GetTextBox(string txtName){
			//Get the type of this screen
			Type FormType = this.GetType();
			
			//Get the field from the type
			FieldInfo field = FormType.GetField(txtName,BindingFlags.NonPublic | BindingFlags.Instance);
			if( field != null ){
			    
				//Use the instance of this screen to get the instance of the field
				Neo.ApplicationFramework.Controls.Controls.Label lb;
				lb = (Neo.ApplicationFramework.Controls.Controls.Label) field.GetValue(this);
					
				//Cast the Label into a Text control. This code was pulled from the TQTERM_A7 function and can be
				//found in the cs file associated with this screen.
				TextControlCFAdapter myTxt;
				myTxt = this.AdapterService.CreateAdapter<Neo.ApplicationFramework.Controls.Script.TextControlCFAdapter>(lb);
				return myTxt;
			}
			return null;
		}
	}
}
TextControlLoop.zip
(24.52 KiB) Downloaded 3306 times
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer

User avatar
Chris T.
Posts: 109
Joined: Mon Nov 20, 2017 5:29 pm

Re: Loop through onscreen controls

Post by Chris T. »

Locked
Best regards,
Christopher
(801) 708-6690
Technical Support
Contact Us

Beijer Electronics AB
http://www.beijerelectronics.us

Locked