Edit control properties by string name

A forum devoted to the discussion of all topics having to do with scripting and other advanced programming using iX Developer.
Post Reply
Chris
Posts: 1
Joined: Thu Nov 22, 2012 6:33 am

Edit control properties by string name

Post by Chris »

Hi,

i don't know if this thread's title is perfect for my question but here we go:

I've got several Text controls on my Screen and would like to know if it's possible to edit their displayed text by just having their name in a string variable.

I mean s.th. like mark.monroe posted in this thread http://ixtalk.beijerelectronics.us/view ... 1149#p1149 but only for Text controls.

i tried to do it like this:

Code: Select all

		private Neo.ApplicationFramework.Controls.Script.TextControlCFAdapter FindControlByName(string name)
		{
			foreach (Neo.ApplicationFramework.Controls.Script.TextControlCFAdapter c in this.Controls) 
			{
				if (c.Name == name)
					return c;
			}
			return null; 
		}
Do you have any idea how one could do that?

Thanks
but it's not working..

User avatar
Edmund
Posts: 92
Joined: Thu Nov 29, 2012 2:27 pm

Re: Edit control properties by string name

Post by Edmund »

Hello Chris!

Not completely sure of what control you mean with TextControlCFAdapter, but maybe you can make something out of this.

Here is a little example I put together.

I the screen there is three buttons.

Button 1 will toggle the visibility of the two other.

Button 2 is hidden at screen opened event.

Button 3 is visible from start.

By finding the buttons via their names we can control them.

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.Collections;
	using System.Windows;
    
    public partial class Screen2
    {
		void Screen2_Opened(System.Object sender, System.EventArgs e)
		{
			Button2.Visible = false;
		}
						
		
		void Button1_Click(System.Object sender, System.EventArgs e)
		{
			ToggleButtonVisibility("Button2");
			ToggleButtonVisibility("Button3");
		}
		
	
		public void ToggleButtonVisibility (string buttonName)
		{
			System.Collections.Generic.List<Neo.ApplicationFramework.Controls.Button> buttons = GetLogicalChildCollection<Neo.ApplicationFramework.Controls.Button>(this);		
			foreach (Neo.ApplicationFramework.Controls.Button button in buttons)
				    if(button.Name == "m_" + buttonName)
					    if(button.IsVisible)
						    button.Visibility = System.Windows.Visibility.Hidden;
					    else
						    button.Visibility = System.Windows.Visibility.Visible;	
		}
	


		public static System.Collections.Generic.List<T> GetLogicalChildCollection<T>(object parent) where T : DependencyObject
		{
			System.Collections.Generic.List<T> logicalCollection = new System.Collections.Generic.List<T>();
			GetLogicalChildCollection(parent as DependencyObject, logicalCollection);
			return logicalCollection;
		}
		
		private static void GetLogicalChildCollection<T>(DependencyObject parent, System.Collections.Generic.List<T> logicalCollection) where T : DependencyObject
		{
			IEnumerable children = LogicalTreeHelper.GetChildren(parent);
			foreach (object child in children)
			{
				if (child is DependencyObject)
				{
					DependencyObject depChild = child as DependencyObject;
					if (child is T)
					{
						logicalCollection.Add(child as T);
					}
					GetLogicalChildCollection(depChild, logicalCollection);
				}
			}
		}
    }
}


Please note that iX adds "m_" to the name of each control (good practice when declaring member variables to a class).
Edmund Andersson

AITECH AB

Part of Beijer Integrator Group

buhl
Posts: 4
Joined: Thu Nov 29, 2012 8:44 am
Location: Aalborg
Contact:

Re: Edit control properties by string name

Post by buhl »

Hello

I was looking for exactly this, but sadly this only works for projects target PC and I'm working on a project for T15B. I want to find a control by searching for its name.

When I compile the example, I get the following error:

The type or namespace name could not be found (are you missing an assembly reference?)

What do you propose, to solve this problem?

Jacob Buhl
Alfa Laval

User avatar
Edmund
Posts: 92
Joined: Thu Nov 29, 2012 2:27 pm

Re: Edit control properties by string name

Post by Edmund »

Well yes the example only works with the full .Net Framework (WPF, PC-Target), and I havent found any good ways to do it for the Compact Framework (CE, Panels).

What you could do is adding the controls to a Array or List that you search in and updates if the object exsits...
Edmund Andersson

AITECH AB

Part of Beijer Integrator Group

buhl
Posts: 4
Joined: Thu Nov 29, 2012 8:44 am
Location: Aalborg
Contact:

Re: Edit control properties by string name

Post by buhl »

Can you please explain that some more because I can't see how I can add them if i can't find them by their name?

I don't know how many object I have on the page because I auto generate them. But in run time I need to rearrange them depending on if some are disabled and therefore not visible. I know that the names are A1, A2, A3 A3 etc.
That is why I wanted to search for the component and loop this while there a component existed with that name.

buhl
Posts: 4
Joined: Thu Nov 29, 2012 8:44 am
Location: Aalborg
Contact:

Re: Edit control properties by string name

Post by buhl »

Normally you would use this function in Compact Framework to locate a control by its name but it does not work.

private Control FindControl(Control parent, string ctlName)
{
foreach(Control ctl in parent.Controls)
{
if( ctl.Name.Equals(ctlName))
{
return ctl;
}
FindControl(ctl, ctlName);
}
return null;
}

andrea
Posts: 72
Joined: Tue Dec 11, 2012 5:44 am

Re: Edit control properties by string name

Post by andrea »

Hi Edmund,

I have tried something similar to what you suggest but Ihave problems on some aspects because our screens have a lot of controls on it and also Background/Parent Screen.

For example suppose that we have Button1 on the actual screen (example named: Screen1) but also Button1 in the Background Screen (example named: Background_Screen1).

I only would like to change the properties of visibility of Screen1.Button1 not for the Background screen's Button1. How to obtain that?

Thank you

Post Reply