WPF with Automation & AddAutomationEventHandler & TextPattern.TextChangedEvent

If I add an AutomationEventHandler on a textBox and if I use the function GetCurrentPattern(TextPattern.Pattern) as TextPattern, when I enter a character into this textbox, I receive a number of event equal at the number of time I called the function GetCurrentPattern.

Have people has the same problem And how I can have just one event by character

Thanks

This is my code

private void RegisterForEvents()

{

if ((bool)element.GetCurrentPropertyValue(AutomationElement.IsTextPatternAvailableProperty))

{

textHandler = new AutomationEventHandler(OnTextChanged); Automation.AddAutomationEventHandler(TextPattern.TextChangedEvent, element, TreeScope.Element, textHandler);

}

}

private void OnTextChanged(object src, AutomationEventArgs e)

{

AutomationElement eElement = src as AutomationElement;

String text = "";

// Get required control patterns

//

// Once you have an instance of an AutomationElement,

// check if it supports the TextPattern pattern.

TextPattern targetTextPattern = selectionitemElement.GetCurrentPattern(TextPattern.Pattern) as TextPattern;

if (targetTextPattern != null)

{

text = targetTextPattern.DocumentRange.GetText(-1);

}

System.Console.Out.WriteLine("OnTextChanged = "+ text);

}



Answer this question

WPF with Automation & AddAutomationEventHandler & TextPattern.TextChangedEvent

  • samssb

    Hi Philippe,

    I'm very new to UI Automation and WPF. I've a problem in trying to access the TextChanged event of the TextBox control. I was looking on the net for help and came across your post.

    I've written exactly the same code as the one you have posted above to add an automation event handler for the TextChanged event of TextPattern. However, my event handler function never gets called even though the text in the edit control is changed.

    Is there anything additional that I have to do Can you pls throw some light on this


  • nares

    There is no guarantee that only one event will be fired for any particular action within the UI. There are too many variables to account for in each scenario. You could also be witnessing the result of an improper UI Automation implementation by the control developer.

    You could filter the events within your event code in some manner—for example, tracking keystrokes or string lengths.

    Karl



  • WPF with Automation & AddAutomationEventHandler & TextPattern.TextChangedEvent