InfoConnect API Guide
Walkthrough / Customize the User Interface / Define Context Menus
In This Topic
    Define Context Menus
    In This Topic

    You can define context menus and assign them at runtime. You can also load context menus or switch from one menu to another when specific screens are loaded.  

    Define a Context Menu

    Switch Context Menus

    Define a Context Menu

    This sample opens InfoConnect, creates a new session, and defines and assigns a context menu.

    1. In Visual Studio, create a new Console Application project and add references for the following InfoConnect assemblies. (Depending on your version of Visual Studio, these can be found either on the .NET tab or under Assemblies | Extensions.)
      Attachmate.Reflection.Framework
      Attachmate.Reflection.Emulation.OpenSystems (if using Open Systems)
      Attachmate.Reflection.Emulation.IbmHosts (if using IBM)
      Attachmate.Reflection
    2. Replace all the code in the Program.cs file with the following code for the terminal you are using. This code creates a new terminal session, and a new context menu, which is assigned to a copy of the existing menu. It creates a menu item and adds it to the menu and removes an item. Then it assigns the new menu as the default menu for the session.  
      Create a context menu
      Copy Code
      using System;
      using System.Collections.Generic;
      using System.Text;
      using Attachmate.Reflection.Framework;
      using Attachmate.Reflection.Emulation.IbmHosts;
      using Attachmate.Reflection.UserInterface;
      using Attachmate.Reflection;
      namespace ContextMenus
      {
          class Program
          {
              static void Main(string[] args)
              {
                  //Start a visible instance of InfoConnect or get the instance running at the given channel name
                  Attachmate.Reflection.Framework.Application app = MyReflection.CreateApplication("myWorkspace", true);
      
                  //Create a Ibm 3270 terminal
                  IIbmTerminal terminal = (IIbmTerminal)app.CreateControl(new Guid("{09E5A1B4-0BA6-4546-A27D-FE4762B7ACE1}"));
      
                  //Specify the host name or IP address and connect.
                  terminal.HostAddress = "demo:ibm3270.sim";
                  terminal.Port = 623;
                  terminal.Connect();
      
                  //Make the session visible               
                  IFrame frame = (IFrame)app.GetObject("Frame");
                  frame.CreateView(terminal);
      
                  //Get the collection of context menus defined for this session
                  IContextMenus menus = (IContextMenus)terminal.ContextMenus;
      
                  //Create a menu and assign it to a copy of the existing default menu
                  ContextMenu AlternateMenu = new ContextMenu("alternateMenu");
                  AlternateMenu = (ContextMenu)menus.DefaultContextMenu;
      
                  //Create an action and add it to a menu item
                  InputMapAction action = new InputMapAction(InputMapActionID.EmailMessageAction, null);
                  InputMapActionSequence sequence = new InputMapActionSequence();
                  sequence.Add(action);
                  ContextMenuItem item = new ContextMenuItem("Create Email", ContextMenuItemType.MenuItem, sequence);
      
                  //Add a line separator and the menu item to the alternate menu
                  AlternateMenu.AddSeparator();
                  AlternateMenu.AddMenuItem(item);
      
                  //Remove the Cut item in the alternate menu based on its position in the menu
                  AlternateMenu.RemoveItem(2);
      
                  //assign the Alternate menu as the context menu for this session
                  menus.DefaultContextMenu = AlternateMenu;
      
                  Console.ReadKey();
              }
          }
      }
                                         
      
      Create a context menu
      Copy Code
      using System;
      using System.Collections.Generic;
      using System.Text;
      using Attachmate.Reflection.Framework;
      using Attachmate.Reflection.Emulation.OpenSystems;
      using Attachmate.Reflection.UserInterface;
      using Attachmate.Reflection;
      namespace ContextMenus
      {
          class Program
          {
              static void Main(string[] args)
              {
                  //Start a visible instance of InfoConnect or get the instance running at the given channel name
                  Attachmate.Reflection.Framework.Application app = MyReflection.CreateApplication("myWorkspace", true);
      
                  //Create an Open Systems terminal
                  ITerminal terminal = (ITerminal)app.CreateControl(new Guid("{BE835A80-CAB2-40d2-AFC0-6848E486BF58}"));
      
                  //Specify the host name or IP address and connect.
                  IConnectionSettingsTelnet conn = (IConnectionSettingsTelnet)terminal.ConnectionSettings;
                  conn.HostAddress = "yourHostName";
                  terminal.Connect();
      
                  //Make the session visible              
                  IFrame frame = (IFrame)app.GetObject("Frame");
                  frame.CreateView(terminal);
      
                  //Get the collection of context menus defined for this session
                  IContextMenus menus = (IContextMenus)terminal.ContextMenus;
      
                  //Initialize a menus as a copy of the existing default menu
                  ContextMenu AlternateMenu = new ContextMenu("alternateMenu");
                  AlternateMenu = (ContextMenu)menus.DefaultContextMenu;
      
                  //Create an action and add it to a menu item
                  InputMapAction action = new InputMapAction(InputMapActionID.EmailMessageAction, null);
                  InputMapActionSequence sequence = new InputMapActionSequence();
                  sequence.Add(action);
                  ContextMenuItem item = new ContextMenuItem("Create Email", ContextMenuItemType.MenuItem, sequence);
      
                  //Add the menu item to the alternate menu
                  AlternateMenu.AddSeparator();
                  AlternateMenu.AddMenuItem(item);
      
                  //Remove an item in the alternate based its position in the menu
                  AlternateMenu.RemoveItem(2);
      
                  //Assign the Default menu for the session to the new Alternate menu
                  menus.DefaultContextMenu = AlternateMenu;
                  Console.ReadKey();
              }
          }
      }
                                         
      
        

    To test this project

    1. Press F5 to compile and run the project.
    2. In the InfoConnect session, right-click to open the context sensitive menu and verify that it has a Create Email item and does not have a Cut item.

    Switch the Context Menu Back and Forth

    You can create an alternate menu and use only on specific screens.

    1. In Visual Studio, create a new Console Application project and add references for the following InfoConnect assemblies. (Depending on your version of Visual Studio, these can be found either on the .NET tab or under Assemblies | Extensions.)Attachmate.Reflection.Framework
      Attachmate.Reflection.Emulation.OpenSystems (if using Open Systems)
      Attachmate.Reflection.Emulation.IbmHosts (if using IBM)
      Attachmate.Reflection
    2. Replace all the code in the Program.cs file with the following code for the terminal you are using. This code adds properties for the two menus we will create; an Alternate menu that we can customize and a copy of the Default menu.
      Add properties for menus
      Copy Code
                                      
      using System;
      using System.Collections.Generic;
      using System.Text;
      using Attachmate.Reflection.Framework;
      using Attachmate.Reflection.Emulation.IbmHosts;
      using Attachmate.Reflection.UserInterface;
      using Attachmate.Reflection;
      namespace ContextMenus
      {
          class Program
          {
              private IContextMenu _alternateMenu;
              private IContextMenu _defaultMenu;
              public IContextMenu AlternateMenu
              {
                  get { return this._alternateMenu; }
                  set { this._alternateMenu = value; }
              }
              public IContextMenu DefaultMenu
              {
                  get { return this._defaultMenu; }
                  set { this._defaultMenu = value; }
              }
              static void Main(string[] args)
              {
      
              }
          }
      }
      
    3. Add the following code to create the menus. This code opens a session and creates two copies of the original menu—an Alternate menu that we can customize and a Default menu that we can reference when we need to switch back to the default. Then it creates a menu item mapped to an email action and adds it to the AlternateMenu. It also removes the Cut item from the menu.  Finally, it creates event handlers that we'll use to swap menus for specific screens and to make sure the session is set to use the default menu if it is automatically saved when it is closed.
      Create the menus
      Copy Code
      static void Main(string[] args)
      {
          Program SwitchMenus = new Program();
          //Start a visible instance of InfoConnect or get the instance running at the given channel name
          Attachmate.Reflection.Framework.Application app = MyReflection.CreateApplication("myWorkspace", true);
        
            
          //Create an IBM 3270 terminal
          string sessionFilePath = System.Environment.GetEnvironmentVariable("USERPROFILE") + @"\Documents\Micro Focus\InfoConnect\demoSession.rd3x";
          IIbmTerminal terminal = (IIbmTerminal)app.CreateControl(sessionFilePath);
      
          //Make the session visible              
          IFrame frame = (IFrame)app.GetObject("Frame");
          frame.CreateView(terminal);
      
          //Get the collection of context menus defined for this session
          IContextMenus menus = (IContextMenus)terminal.ContextMenus;
      
          //Initialize two menus as copies of the existing default menu
          SwitchMenus.AlternateMenu = new ContextMenu("alternateMenu");
          SwitchMenus.AlternateMenu = menus.DefaultContextMenu;
          SwitchMenus.DefaultMenu = new ContextMenu("defaultMenu");
          SwitchMenus.DefaultMenu = menus.DefaultContextMenu;
      
          //Create an action and add it to a menu item
          InputMapAction action = new InputMapAction(InputMapActionID.EmailMessageAction, null);
          InputMapActionSequence sequence = new InputMapActionSequence();
          sequence.Add(action);
          ContextMenuItem item = new ContextMenuItem("Create Email", ContextMenuItemType.MenuItem, sequence);
      
          //Add the menu item to the alternate menu
          SwitchMenus.AlternateMenu.AddSeparator();
          SwitchMenus.AlternateMenu.AddMenuItem(item);
      
          //Remove an item in the alternate menu based its position in the menu
          SwitchMenus.AlternateMenu.RemoveItem(2);
      
          //Set event handler for NewScreenReady event so we can identify screens and swap menus for specific screens
          IIbmScreen screen = terminal.Screen;
          screen.NewScreenReady += SwitchMenus.screen_NewScreenReady;
      
          //Set event handler for BeforeDisconnect event to make sure session is saved with the correct default menu
          terminal.BeforeDisconnect += SwitchMenus.terminal_BeforeDisconnect;
      
          Console.ReadKey();
      }
      
      Create the menus
      Copy Code
      static void Main(string[] args)
      {
          Program SwitchMenus = new Program();
      
          //Create an application object
          Attachmate.Reflection.Framework.Application reflectionApplication = MyReflection.CreateApplication("myWorkspace", true);
      
          //Create a VT terminal
          string sessionPath = System.Environment.GetEnvironmentVariable("USERPROFILE") + @"\Documents\Micro Focus\InfoConnect\demoSession.rdox";
          ITerminal terminal = (ITerminal)reflectionApplication.CreateControl(sessionPath);
      
          //Make the session visible              
          IFrame frame = (IFrame)reflectionApplication.GetObject("Frame");
          frame.CreateView(terminal);
      
          //Get the collection of context menus defined for this session
          IContextMenus menus = (IContextMenus)terminal.ContextMenus;
      
          //Initialize two menus as copies of the existing default menu
          SwitchMenus.AlternateMenu = new ContextMenu("alternateMenu");
          SwitchMenus.AlternateMenu = menus.DefaultContextMenu;
          SwitchMenus.DefaultMenu = new ContextMenu("defaultMenu");
          SwitchMenus.DefaultMenu = menus.DefaultContextMenu;
      
          //Create an action and add it to a menu item
          InputMapAction action = new InputMapAction(InputMapActionID.EmailMessageAction, null);
          InputMapActionSequence sequence = new InputMapActionSequence();
          sequence.Add(action);
          ContextMenuItem item = new ContextMenuItem("Create Email", ContextMenuItemType.MenuItem, sequence);
      
          //Add the menu item to the alternate menu
          SwitchMenus.AlternateMenu.AddSeparator();
          SwitchMenus.AlternateMenu.AddMenuItem(item);
      
          //Remove an item in the alternate based its position in the menu
          SwitchMenus.AlternateMenu.RemoveItem(2);
      
          //Set event handler for ControlKeySending event so we can identify screens and swap menus for specific screens
          IScreen screen = terminal.Screen;
          screen.ControlKeySending += SwitchMenus.screen_ControlKeySending;
      
          //Set event handler for Closing event to make sure session is saved with the correct default menu
          terminal.Closing += SwitchMenus.terminal_Closing;
          Console.ReadKey();
      }
      
    4. Set up the first event to switch menus when a specific command is recognized (for Open Systems) or when a certain screen is recognized.       
      Handle NewScreenReady event to swap menus
      Copy Code
      public void screen_NewScreenReady(object sender, EventArgs e)
      {
          IIbmScreen screen = (IIbmScreen)sender;
          IIbmTerminal terminal = screen.Parent;
      
          //Get a string that is unique to the second screen in the demo and use it to identify this screen
          string screenID1 = screen.GetText(1, 1, 5);
      
          //Use the alternate menu only on this screen
          if (screenID1 == "LOGON")
          {
              terminal.ContextMenus.DefaultContextMenu = this.AlternateMenu;
          }
          else
          {
              terminal.ContextMenus.DefaultContextMenu = this.DefaultMenu;
          }
      }
      
      Handle ControlKeySending event to swap menus
      Copy Code
      public void screen_ControlKeySending(object sender, ControlKeySendingEventArgs e)
      {
          IScreen screen = (IScreen)sender;
          ITerminal terminal = screen.Parent;
      
          //Apply the alternate menu only after the 'demodata' command is entered
          if (screen.GetText(screen.CursorRow, 1, 25).Contains("demodata"))
          {
              terminal.ContextMenus.DefaultContextMenu = AlternateMenu;
          }
          else
          {
              terminal.ContextMenus.DefaultContextMenu = DefaultMenu;
          }
      }
      
    5. Set up the second event to make sure the session is set to use the default menu when it is saved. We need to handle this event to prevent changing the default menu settings if the InfoConnect workspace is set to save document settings automatically when closing a document. (In our sample, if the session is closed when the alternate menu is used, the alternate menu is saved as the default menu for this session unless we handle this event.)         
      Save before disconnecting
      Copy Code
      //Set the session to use the default menu and save it before disconnecting or closing
      private void terminal_BeforeDisconnect(object sender, EventArgs e)
      {
          IIbmTerminal terminal = (IIbmTerminal)sender;
          terminal.ContextMenus.DefaultContextMenu = this.DefaultMenu;
          terminal.Save();
      }
      
      Save before closing
      Copy Code
      //Set the session to use the default menu and save it before closing
      private void terminal_Closing(object sender, CancelableEventArgs e)
      {
          ITerminal terminal = (ITerminal)sender;
          terminal.ContextMenus.DefaultContextMenu = this.DefaultMenu;
          terminal.Save();
      }
      

    To test this project

    1. Press F5 to run the sample
    2. When the session opens, right-click on the screen to view the context menu and verify that the default menu is displayed.
    3. Enter any credentials to log on to the demo.
    4. If you are using Open Systems, enter "demodata" to go to a data screen.
    5. Right-click on the screen and verify that it has a Create Email item and does not have a Cut item.