0

I'm having trouble figuring out how to get c# to get the focused element within a window. I've a window of another program with two text boxes, a dropdown, and two buttons. I need to be able to open this program, pull it to the front, and fill in the two text boxes and select from the dropdown programatically.

I can pull the window forward with SetForegroundWindow and fill things out and navigate about with SendKeys, but since I don't always know which box the cursor will start in, I need to be able to determine where it is once I've pulled it to the front to fill them in properly.

Any ideas?

  • WinForms, WPF? Wouldn't one of the TextBoxes have the focus already when you type? – LarsTech Jun 01 '16 at 17:17
  • are you using [UIAutomation](https://msdn.microsoft.com/en-us/library/windows/desktop/ee684009.aspx)? – muratgu Jun 01 '16 at 17:20
  • I've edited for (hopefully) a little more clarity. The UI box isn't mine—it's part of an external piece of proprietary software. One of the boxes does have focus, but the script I'm working on now needs to know which one to fill them in properly. I am not using UIAutomation. – pucedragonlord Jun 01 '16 at 18:41

1 Answers1

1

Going off of memory and pseudocode here.

Inside of a form, you should be able to use this.ActiveControl to get that information, which should provide you with a wealth of other information, such as the name of the control, type, etc.

Zoey M
  • 714
  • 5
  • 17
  • 1
    I'll look into that. Can this/Control.ActiveControl be used on external applications? This window I'm working with isn't being made by my code, just interacted with, which is where some of the difficulty is coming from. The most I've gotten out of the window so far is the IntPtr of the window itself, but I'm not sure what all I can do with that, yet. – pucedragonlord Jun 01 '16 at 18:55
  • Wirth trying externally, I honestly have never had that use case, so I'd hate to steer you in the wrong direction. If I try it, I'll comment back for sure! – Zoey M Jun 01 '16 at 18:57
  • 1
    Digging through MSDN, it looks like it's on the right track, at least. All of the active/focus/keyboard focus get functions I've tested so far just give me the window. The Control properties and methods at least deal with the actual UI elements. – pucedragonlord Jun 01 '16 at 19:02