iX Talk
http://beijerinc.com/support/ix/forum/

How to convert a string to a Screen or Tag.
http://beijerinc.com/support/ix/forum/viewtopic.php?f=9&t=87
Page 1 of 1

Author:  Ron L. [ Thu Sep 22, 2011 11:32 am ]
Post subject:  How to convert a string to a Screen or Tag.

You will need to include these namespace references.

Code:
   using Neo.ApplicationFramework.Generated;     
   using System.Reflection;


Function for converting a string to a Tag.

Code:
// 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:
      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:
//--------------------------------------------------------------
// 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:
ScriptModule1.ShowScreen("Screen2");

Page 1 of 1 All times are UTC - 7 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/