Page 1 of 1

How to convert a string to a Screen or Tag.

Posted: Thu Sep 22, 2011 11:32 am
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");

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

Posted: Tue Jan 21, 2020 5:35 pm
by Chris T.
Locked