Reflection 2014 .NET API Guide
Get the View that has Focus

This sample program gets the host session document that has focus in the Reflection workspace by getting the view object for that session.

Note: This program works only with host session documents; not Web page documents.

 

To get the view that has focus in the Workspace

  1. Start Reflection, and create two or more session documents that connect to different hosts.
  2. In Visual Studio, create a new Console Application project.
  3. In the New Project dialog box, type Get View in Focus in the Name field.
  4. Select the Create directory for solution box, click Browse to select your API projects folder (for example, C:\MyProjects), and then click OK.
  5. In Solution Explorer, add the class assemblies. (For instructions, see Create an API Project.)
  6. Copy the code from one of the following programs:

      

    using System;

    using System.Collections.Generic;

    using System.Text;

    using Attachmate.Reflection.Framework;

    using Attachmate.Reflection.UserInterface;

    using Attachmate.Reflection.Emulation.IbmHosts;

     

    //GetFocusedSession finds which session has the focus in a Reflection workspace.

    //Before you run this sample, open Reflection from the Start menu and then connect to two or more sessions.

    namespace GetFocusedSession

    {

      class Program

      {

        static void Main(string[] args)

        {

          Attachmate.Reflection.Framework.Application reflectionApplication = MyReflection.CreateApplication();

          if (reflectionApplication != null)

          {

            IView sessionView;

            //Get the Frame object.

            IFrame theFrame = (IFrame)reflectionApplication.GetObject("Frame");

            if (theFrame != null)

            {

              //Get the session view with the focus.

              sessionView = theFrame.SelectedView;

              if (sessionView != null)

              {

                //Get the control embedded in the view.

                IIbmTerminal terminal = (IIbmTerminal)sessionView.Control;

                if (terminal != null)

                {

                  IIbmScreen screen = terminal.Screen;

                  screen.WaitForHostSettle(6000, 3000);

                  string text = screen.GetText(18, 2, 48);

                  Console.WriteLine("The Text at row 18, column 2: " + text);

                  Console.WriteLine("The session is connected to: " + terminal.HostAddress);

                }

                else

                  Console.WriteLine("Fatal error. Check Reflection installation.");

              }

              else

                Console.WriteLine("No session is open in Reflection.");

            }

            else

              Console.WriteLine("Fatal error. Check Reflection installation.");

          }

          else

            Console.WriteLine("Failed to get Application object.");

        }

      }

    }

                   

     

                                                                                    

    using System;

    using System.Collections.Generic;

    using System.Text;

    using Attachmate.Reflection.Framework;

    using Attachmate.Reflection.UserInterface;

    using Attachmate.Reflection.Emulation.OpenSystems;

     

    //GetFocusedSession finds which session has the focus in a Reflection workspace.

    //Before you run this sample, open Reflection from the Start menu and then connect to two or more sessions.

    namespace GetFocusedSession

    {

      class Program

      {

        static void Main(string[] args)

        {

          Attachmate.Reflection.Framework.Application reflectionApplication = MyReflection.CreateApplication();

          if (reflectionApplication !=null)

          IView sessionView;

          //Get the Frame object.

          IFrame theFrame = (IFrame)reflectionApplication.GetObject("Frame");

          if (theFrame != null)

          {

            //Get the session view with the focus.

            sessionView = theFrame.SelectedView;

            if (sessionView != null)

            {

              //Get the control embedded in the view.

              ITerminal terminal = (ITerminal)sessionView.Control;

              if (terminal != null)

              {

                IScreen screen = terminal.Screen;

                IConnectionSettingsTelnet conn = (IConnectionSettingsTelnet)terminal.ConnectionSettings;

                string text = screen.GetText(18, 2, 48);

                Console.WriteLine("The Text at row 18, column 2: " + text);

                Console.WriteLine("The session is connected to: " + conn.HostAddress);

              }

              else

                Console.WriteLine("Fatal error. Check Reflection installation.");

            }

            else

              Console.WriteLine("No session is open in Reflection.");

          }

          else

            Console.WriteLine("Fatal error. Check Reflection installation.");

        }

        else

          Console.WriteLine("Failed to get Application object.");

        }

      }

    }

  7. In Visual Studio, paste the code to the Program.cs tab, replacing all existing code.
  8. Build the application (Build > Rebuild Solution) and then run it (Debug > Start Without Debugging).

The host address of the selected session should display in the console. If you select a different session in the Reflection workspace and then run the program again, a different host address will display in the console.