Best way to attach an event handler to a control that don't exists yet?

Aug 17, 2012 at 6:05 PM

I need to run an automation only when certain control is visible on a hosted external application (VB6 + .NET 1.1) and that control is only created after certain interaction within the hosted app. Off course if I just use the RegisterActionForEvent in a default action it will fail because the control doesn't exists at the time and I have no control when the client application will create it, that's why I need to monitor for it.

What I've tried so far is with an AutomationEventHandler listening to every new window, check if the title of the window is the one where the control should be and then look for it using AutomationElement.FindFirst with a PropertyCondition with it's name. That works but only when the external application starts, after that there's no Window event to listen to so I'm looking into the Focus event. With the focus event I'm able to search for the control every time something rises that event, but I got the feeling that this approach can be negative to the overall performance of the application.

Any advice?

Thanks.