How to convert a string to a Screen or Tag.

Locked
User avatar
Ron L.
Posts: 214
Joined: Fri Jul 15, 2011 3:21 pm

How to convert a string to a Screen or Tag.

Post by Ron L. »

You will need to include these namespace references.

Code: Select all

   using Neo.ApplicationFramework.Generated;      
   using System.Reflection;
Function for converting a string to a Tag.

Code: Select all

// returns null if tag not found
      public GlobalDataItem StringToTag(string tagName)
      {
         string typeName = (new GlobalDataItem()).GetType().Name;
         PropertyInfo[] props = Globals.Tags.GetType().GetProperties();
         foreach (PropertyInfo prop in props) {
            if (prop.PropertyType.Name.Equals(typeName)) {
               if (prop.Name.Equals(tagName, StringComparison.CurrentCultureIgnoreCase)) {
                  return (GlobalDataItem)prop.GetValue(Globals.Tags, null);
               }               
            }
         }
         return null;
      }
Example function for converting a string to a Screen.

Code: Select all

      private static IScreenAdapter StringToScreen(string name)
      {         
         Type globalType = typeof(Globals);
         Type iAdaptType = typeof(IScreenAdapter);
         foreach (var p in globalType.GetProperties())
         {            
            if (p.PropertyType.Name.Equals(iAdaptType.Name) &&
               p.Name.Equals(name))
            {
               return (IScreenAdapter)p.GetValue(globalType, null);
            }
         }
         return null;
      }
Some code inside a script module that uses StringToScreen.

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 Neo.ApplicationFramework.Generated;      
   using System.Reflection;
   
    public partial class ScriptModule1
    {
      public static void ShowScreen(string name)
      {
         IScreenAdapter scrn = StringToScreen(name);
         if (scrn == null) {
            return;
         }
         scrn.Show();
      }
      
      private static IScreenAdapter StringToScreen(string name)
      {         
         Type globalType = typeof(Globals);
         Type iAdaptType = typeof(IScreenAdapter);
         foreach (var p in globalType.GetProperties())
         {            
            if (p.PropertyType.Name.Equals(iAdaptType.Name) &&
               p.Name.Equals(name))
            {
               return (IScreenAdapter)p.GetValue(globalType, null);
            }
         }
         return null;
      }
    }
}
The code to use ShowScreen looks like this.

Code: Select all

ScriptModule1.ShowScreen("Screen2");
Best Regards,

Beijer Electronics, Inc.
Ron Lloyd | Applications Engineer

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

Re: How to convert a string to a Screen or Tag.

Post by Chris T. »

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

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

Locked