2

Using AutomationElement, is there any way send clicks to a TabItem without having to move the mouse and simulate clicks? AutomationElement is still new to me - as far as I understand it, unless it supports the InvokePattern (which TabItem does not) you have to go the route of locating the control location and simulating the mouse. I have that code working (see below) - I am just curious if this is my option.

AutomationElement tabControl = GetControl(window, "NOTEBOOK");
AutomationElement tabGeneral = GetControl(tabControl, "FM_STAFF_SUB_P1");

AutomationElementCollection tabs = GetAllTabs(window, tabGeneral);

System.Windows.Point p = tabs[1].GetClickablePoint();

MoveMouse((int)p.X, (int)p.Y);
ClickMouse();

Thank you.

duck
  • 747
  • 14
  • 31

2 Answers2

1
  1. Try tab.SetFocus()
  2. Get all supported patterns (tab.GetSupportedPatterns()), then see which ones are supported for this tab implementation. It should support SelectionItemPattern, so use: ((SelectionItemPattern)tab.GetCurrentPattern(SelectionItemPattern.Pattern)).Select()
  3. Use SendKeys to the window in order to navigate the tabs (most cases will have a hot key to navigate between them. You can combine by checking after each navigation if the tab is selected.
  4. If all the above fail, I guess mouse click is your only option.
DoronG
  • 2,576
  • 16
  • 22
1

I had a similar problem with adding hot keys to tab items. In my case just selecting the tab item would give it focus but would not show the content of the tab when it was dynamically generated. Unless I misunderstand your question, this example will simulate a tab item click with TabItemAutomationPeer.

        //get the TabItem
      TabItem tabItem = (TabItem)sender; //or however you are getting it.
    
        //get the TabControl
      TabControl tabControl = UIHelper.FindLogicalParent<TabControl>(tabItem); //or however you are getting it.
      
        //do that magic
      tabItem.IsSelected = true; 
      TabControlAutomationPeer tabControlAutomationPeer = new TabControlAutomationPeer(tabControl);
      TabItemAutomationPeer tabItemAutomationPeer = new TabItemAutomationPeer(tabItem, tabControlAutomationPeer);
      tabItemAutomationPeer.SetFocus(); //works like a click
bbedson
  • 133
  • 1
  • 8
  • Wooo blast from the past! Your solution also did not work sadly. The more and more stuff I tried I was finally able to get the tab to be selected so it loaded the tab page - but then I was unable to click the controls on it. It gave me errors like "Control xxx does not exist" I eventually just went the route of moving the mouse. I should also add that I was clicking tabs on a 3rd party application so who knows exactly why it was crashing. – duck Sep 03 '20 at 20:31