locked
UI Automation issue isung the Tab Control. RRS feed

  • Question

  • Hi All,

    I have one issue here with the Tab Control...

    The problem is that after the user clicks on one tab, if they select another tab that is not getting a focus change... I ONLY get ONE UNIQUE focus change when the user clicks on the first tab.... and I need to get the focus change when the user clicks each Tab, each time...

    Any ideas about how to solve this ?

    Thanks a lot, in advance.
    Best regards.

    • Moved by Harry Zhu Monday, August 17, 2009 8:28 AM (From:Visual C# General)
    Monday, August 10, 2009 5:04 PM

Answers

  • Hi Richard,

    Do you mean to detect the selection changes on the TabControl from the UI Automation application? If so, you can try something like this:

    private void button1_Click(object sender, RoutedEventArgs e)
            {
                Thread automateThread = new Thread(new ThreadStart(Automate));
                automateThread.Start();
            }
    
            private void Automate()
            {
                AutomationElement rootElement = AutomationElement.RootElement;
                if (rootElement != null)
                { 
                    PropertyCondition condition = 
                        new PropertyCondition(AutomationElement.NameProperty, "Window1");
    
                    AutomationElement appElement = rootElement.FindFirst(TreeScope.Children, condition);
    
                    if (appElement != null)
                    {
    
                        condition = new PropertyCondition(AutomationElement.AutomationIdProperty, "tabControl1");
                        AutomationElement tabElement = appElement.FindFirst(TreeScope.Descendants, condition);
    
                        Automation.AddAutomationEventHandler(
                            SelectionItemPattern.ElementSelectedEvent,
                            tabElement,
                            TreeScope.Element | TreeScope.Descendants,
                            new AutomationEventHandler(TargetSelectionHandler));
                         
                    }
                }
            }
    
            private void TargetSelectionHandler(object src, AutomationEventArgs e)
            {
                AutomationElement sourceItem = src as AutomationElement;
                Console.WriteLine("Item {0} is selected.", sourceItem.Current.Name);
            }


    Hope this helps.

    Best Regards,
    Zhi-Xin


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
    • Marked as answer by Zhi-Xin Ye Thursday, August 20, 2009 2:34 AM
    Tuesday, August 18, 2009 9:27 AM

All replies

  • You want to use the SelectedIndexChanged event.

    Monday, August 10, 2009 5:13 PM
  • Pls repharse your question. Unable to understand.
    TK
    Monday, August 10, 2009 5:33 PM
  • Thanks for your comments:

    Reply for ScottyDoesKnow:
    Ok,... one more quesrion... once I get the SelectedIndexChanged event, how cn I send that event to my UI Automation code in order to detect that change ?

    Reply for Talal Khan:
    I'm using one Tab Control... and I'm using UI Automation to handle the control... my problem is that,.. UI Automation is not detecting/getting the Tab's focus change event, after the user clicks on one tab ( the first, if you like ), is much clear now ? Sorry if I wasn't clear.


    Thank you all.
    Best regards.
    Monday, August 10, 2009 5:42 PM
  • When you said UI Automation I thought it was just a general term, are you using some library called UI Automation or something?

    Monday, August 10, 2009 5:57 PM
  • No, just the common UI Automation stuff... the basic required UI Automation code, like the Tree Structure, Properties, Control Patterns, etc...
    http://msdn.microsoft.com/en-us/library/ms754207.aspx

    ...about the SelectedIndexChanged,.... I don't what to know when the tab is changed... I want/need to know when the focus is on the tab.

    Thank you.
    Best regards.
    Monday, August 10, 2009 6:03 PM
  • Yes, your question is creating a lot of confusion.  UI automation is a WPF feature.  It does have a TabControl but not a SelectedIndexChanged event.  The best way to avoid this kind of confusion is to post WPF questions in the WPF forum.

    If this is anything like the Windows Forms control, it has a focus event for the TabControl itself but separate focus events for each individual tab page.
    Hans Passant.
    Monday, August 10, 2009 6:34 PM
  • Ya my bad, I usually assume it's a winforms question. I'm not very familiar with WPF but I'd think you can use the Focus event to check when focus is on the control and the SelectionChanged event to keep track of which tab is open. Then you'll know when focus is on a specific tab. This is unless WPF let's you focus on a tab without switching to it, in which case I can't help you.
    Monday, August 10, 2009 7:12 PM
  • Hi Richard,

    Do you mean to detect the selection changes on the TabControl from the UI Automation application? If so, you can try something like this:

    private void button1_Click(object sender, RoutedEventArgs e)
            {
                Thread automateThread = new Thread(new ThreadStart(Automate));
                automateThread.Start();
            }
    
            private void Automate()
            {
                AutomationElement rootElement = AutomationElement.RootElement;
                if (rootElement != null)
                { 
                    PropertyCondition condition = 
                        new PropertyCondition(AutomationElement.NameProperty, "Window1");
    
                    AutomationElement appElement = rootElement.FindFirst(TreeScope.Children, condition);
    
                    if (appElement != null)
                    {
    
                        condition = new PropertyCondition(AutomationElement.AutomationIdProperty, "tabControl1");
                        AutomationElement tabElement = appElement.FindFirst(TreeScope.Descendants, condition);
    
                        Automation.AddAutomationEventHandler(
                            SelectionItemPattern.ElementSelectedEvent,
                            tabElement,
                            TreeScope.Element | TreeScope.Descendants,
                            new AutomationEventHandler(TargetSelectionHandler));
                         
                    }
                }
            }
    
            private void TargetSelectionHandler(object src, AutomationEventArgs e)
            {
                AutomationElement sourceItem = src as AutomationElement;
                Console.WriteLine("Item {0} is selected.", sourceItem.Current.Name);
            }


    Hope this helps.

    Best Regards,
    Zhi-Xin


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
    • Marked as answer by Zhi-Xin Ye Thursday, August 20, 2009 2:34 AM
    Tuesday, August 18, 2009 9:27 AM