Currently focused control

Best practices, code snippets for common functionality, examples, and guidelines.
BCTest
Posts: 88
Joined: Tue Jun 03, 2014 10:15 am
Location: Hamburg, Germany

Currently focused control

Post by BCTest » Tue Jan 26, 2016 10:22 am

I found this simple code in this forum to access the currently activated window:
//Search for the active foreground window and checks if "active" state is true
Ranorex.Form myActiveForm = Host.Local.FindSingle("/form[@active='True']");
Is it possible to access the currently focused control in this Form?
It could be a button, an edit-control, a combobox or whatever...

Regards,
Thomas

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3930
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Currently focused control

Post by odklizec » Tue Jan 26, 2016 10:49 am

Hi,

Try something like this...
Ranorex.Form myActiveForm = Host.Local.FindSingle("/form[@active='True']/*[@accessiblestate~'Focused']");
This should return first focused element on active form.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

BCTest
Posts: 88
Joined: Tue Jun 03, 2014 10:15 am
Location: Hamburg, Germany

Re: Currently focused control

Post by BCTest » Tue Jan 26, 2016 11:44 am

odklizec wrote:Hi,

Try something like this...
Ranorex.Form myActiveForm = Host.Local.FindSingle("/form[@active='True']/*[@accessiblestate~'Focused']");
This should return first focused element on active form.
Hello odklizec,

Thanks for your fast and proper answer: exactly what I want!
Ranorex.Core.Element activeElement = Host.Local.FindSingle("/form[@active='True']/*[@accessiblestate~'Focused']");
Regards,
Thomas

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3930
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Currently focused control

Post by odklizec » Tue Jan 26, 2016 12:30 pm

Cool. I'm glad I could help ;)
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration