cancel
Showing results for 
Search instead for 
Did you mean: 

C# Iterating Controls in the GuiUserArea

Former Member
0 Kudos

I am in the process of building an application in C# winforms.

If we think along the lines of meta data, I am trying to capture as much information about the user's open sessions as I can.


I have got as far as obtaining all the session info that I require.


Now I am curious as to how much information about the visible on-screen data I can capture.
My instinctive thought was to iterate the controls and seeing if I could capture text boxes and labels.

However, I am struggling.

Looking at documentation, I see there are various 'FindBy' functions, but all of these appear to be designed to be useful only if you know the names of individual controls already.


Does anyone know how I can walk/iterate through all the controls in the user area, and capture their name/value ?


Many Thanks for any tips !

🙂




Code so far ....



class sap_helper


{

    

public  static Dictionary<string, SapInfoItem> get_sapInfo()

    

{

        

Dictionary<string, SapInfoItem> sapInfo = new Dictionary<string, SapInfoItem>();

        

GuiApplication sapGuiApp;


        

SapROTWr.CSapROTWrapper sapROTWrapper = new SapROTWr.CSapROTWrapper();

        

object SapGuilRot = sapROTWrapper.GetROTEntry("SAPGUI");


        

object engine = SapGuilRot.GetType().InvokeMember("GetSCriptingEngine", System.Reflection.BindingFlags.InvokeMethod,

            

null, SapGuilRot, null);


        

sapGuiApp = engine as GuiApplication;


        

int i = sapGuiApp.Children.Count;


        

if (sapGuiApp.Connections.Length > 0)

        

{

            

GuiConnection connection = sapGuiApp.Connections.ElementAt(0) as GuiConnection;


           

foreach (GuiSession child_session in connection.Children)

            

{

                

GuiSession session = child_session as GuiSession;   //connection.Children.ElementAt(0) as GuiSession;


// populate user defined structure

                

SapInfoItem sii = new SapInfoItem();  

                

sii.system_name = session.Info.SystemName;

                

sii.client = session.Info.Client;

                

sii.program = session.Info.Program;

                

sii.screen_number = session.Info.ScreenNumber;

                

sii.handle = session.ActiveWindow.Handle;

                

sii.transaction = session.Info.Transaction;

                 // add structure to dictionary collection

                

sapInfo.Add(sii.handle.ToString("X"), sii);

            

}

        

connection = null;

        

}

        

sapGuiApp = null;

        

SapGuilRot = null;

        

sapROTWrapper = null;


    

return sapInfo;

    

}

   

 


}


Accepted Solutions (0)

Answers (1)

Answers (1)

stefan_schnell
Active Contributor

Hello Simon,

sure, as you can see it is very easy to understand the scan process. It is a recursive call with the sub procedure GetIDs. In this sub procedure I add to outText variable the ID, Name and Text (Value) of the control.I start the scan in the main sub procedure with a collection of a session.

'-Begin-----------------------------------------------------------------

'-Directives------------------------------------------------------------
Option Explicit

Dim outText

'-Sub GetIDs------------------------------------------------------------
Sub GetIDs(collection)

  On Error Resume Next

  Dim i, Child

  If InStr(1, collection.Id(), "/usr/", 1) Then
    outText = outText & collection.Id() & vbCrLf
    'outText = outText & collection.Name() & vbCrLf
    'outText = outText & collection.Text() & vbCrLf
  End If


  Set Child = collection.Children()
  If IsObject(Child) Then
    If Child.Count() > 0 Then
      For i = 0 To Child.Count() - 1
        GetIDs Child.Item(CInt(i))
      Next
    End If
  End If


End Sub

'-Sub Main--------------------------------------------------------------
Sub Main()

  Dim SapGuiAuto, application, connection, session, i

  Set SapGuiAuto = GetObject("SAPGUI")
  If Not IsObject(SapGuiAuto) Then
    Exit Sub
  End If

  Set application = SapGuiAuto.GetScriptingEngine
  If Not IsObject(application) Then
    Exit Sub
  End If

  Set connection = application.Children(0)
  If Not IsObject(connection) Then
    Exit Sub
  End If

  Set session = connection.Children(0)
  If Not IsObject(session) Then
    Exit Sub
  End If

  For i = 0 To session.Children().Count() - 1
    GetIDs session.Children(CInt(i))
  Next


End Sub

'-Main------------------------------------------------------------------
Main()
MsgBox outText

'-End-------------------------------------------------------------------

In GetIDs function I catch the GuiUserArea and work only with the controls inside it. The GUIUserArea has an ID like this - /app/con[0]/ses[0]/wnd[0]/usr/... Hope it is no problem to transfer it in C# syntax.

Here an example from the SAP logon screen.

Logon.jpg

Enjoy it.

Cheers

Stefan

davidpz
Explorer

Thank you, didn't expect it used Recursion, functions perfect for me.

Private Sub EnfoqueObjeto(collection, busqueda As String)
'i shoud convert it to a boolean recursion to exit when it's found the string
'-Directives------------------------------------------------------
On Error Resume Next
'-Variables-------------------------------------------------------
Dim i, Child
'Debug.Print "-------"
If InStr(1, collection.ID(), "/usr/", 1) Then
'  Debug.Print collection.ID()
'  Debug.Print "#  " & collection.Name()
'  Debug.Print "@  " & collection.Text()
  If collection.Text() = busqueda Then
    'Debug.Print collection.ID()
    collection.SetFocus
  End If
Else
End If
Set Child = collection.Children()
If IsObject(Child) Then
  If Child.Count() > 0 Then
    For i = 0 To Child.Count() - 1
    EnfoqueObjeto Child.Item(CInt(i)), busqueda
    Next
  End If
End If
End Sub
'call...    EnfoqueObjeto objSess.FindById("wnd[1]"), "/LISTA PMS"