Brocken Tree-Element in VisualStudio 2015

MypkaXD 0 Reputation points
2025-02-01T15:22:23.98+00:00

Hello. I have encountered the following issue. In Visual Studio 2022 (1-st and 2-nd images), I was able to parse window elements using the AutomationElements library in C#. However, in Visual Studio 2015 (3-rd image), this does not work because the tree-grid element structure is broken.User's image

User's image

User's image

Code:

AutomationElement mainWindow = AutomationElement.FromHandle((IntPtr)customToolWindow.HWnd);

PrintAutomationElement(mainWindow, ref variables);

private void GetElementsFromTreeGreed(AutomationElement element, ref ObservableCollection<Variable> variables)

    {

        string name = element.Current.Name;

        if (element.Current.ClassName == "TreeGridItem")

        {

            var expressionForTypeAndName = m_DTE_Dte.Debugger.GetExpression(name, true, 1);

            var expressionForAddress = m_DTE_Dte.Debugger.GetExpression("&(" + name + ")", true, 1);

            if (expressionForTypeAndName.IsValidValue && expressionForAddress.IsValidValue)

            {

                Variable variable = new Variable()

                {

                    m_B_IsAdded = false,

                    m_B_IsSelected = false,

                    m_S_Name = expressionForTypeAndName.Name,

                    m_S_Source = "WatchWindow",

                    m_S_Type = expressionForTypeAndName.Type.Replace(" ", ""),

                    m_S_Addres = expressionForAddress.Value.Split(' ')[0],

                    m_C_Color = new Utils.Color(0, 255, 0)

                };

                if (!isContainVariable(variable, variables))

                    variables.Add(variable);

                else

                    System.Windows.MessageBox.Show("ERROR: A variable with this address: " + variable.m_S_Addres + " is already in the table.\nIt will not be added to it.");

            }

        }

        else

            return;

    }

    private void PrintAutomationElement(AutomationElement element, ref ObservableCollection<Variable> variables)

    {

        if (element.Current.ClassName == "TreeGrid")

        {

            var children = element.FindAll(TreeScope.Children, System.Windows.Automation.Condition.TrueCondition);

            foreach (AutomationElement child in children)

                if (child.Current.ClassName == "TreeGridItem")

                    GetElementsFromTreeGreed(child, ref variables);

        }

        else

        {

            var children = element.FindAll(TreeScope.Children, System.Windows.Automation.Condition.TrueCondition);

            foreach (AutomationElement child in children)

                PrintAutomationElement(child, ref variables);

        }

    }
Visual Studio Extensions
Visual Studio Extensions
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Extensions: A program or program module that adds functionality to or extends the effectiveness of a program.
244 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.