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);
}

WPF with Automation & AddAutomationEventHandler & TextPattern.TextChangedEvent
samssb
Hi Philippe,
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