Welcome Guest ( Login | Register )



All times are UTC - 7 hours [ DST ]



Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Edit control properties by string name
PostPosted: Thu Nov 22, 2012 6:43 am 

Joined: Thu Nov 22, 2012 6:33 am
Posts: 1

Offline
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 https://www.beijerelectronicsinc.com/support/ix/forum/viewtopic.php?p=1149#p1149 but only for Text controls.

i tried to do it like this:
Code:
      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..


 Profile  
 
 Post subject: Re: Edit control properties by string name
PostPosted: Fri Mar 15, 2013 3:26 am 
User avatar

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

Offline
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:
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


 Profile  
 
 Post subject: Re: Edit control properties by string name
PostPosted: Sat Apr 06, 2013 6:34 am 

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

Offline
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


 Profile  
 
 Post subject: Re: Edit control properties by string name
PostPosted: Mon Apr 08, 2013 8:59 am 
User avatar

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

Offline
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


 Profile  
 
 Post subject: Re: Edit control properties by string name
PostPosted: Mon Apr 08, 2013 12:16 pm 

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

Offline
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.


 Profile  
 
 Post subject: Re: Edit control properties by string name
PostPosted: Mon Apr 08, 2013 12:40 pm 

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

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


 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 7 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: