2

Closed

AutomationElementCollectionEnumerator doesn't return the first item

description

To reproduce, just try to iterate over an AutomationElementCollection (using foreach). e.g.:
 
AutomationElementCollection coll = windowElement.FindAll(...);
// Returns elem0, elem1, elem2, elem3
 
foreach (AutomationElement elem in in)
{
AddToList(elem.Name)
// Adds only elem1, elem2, elem3
}
 
To fix it modify AutomationElementCollection.cs:
// AutomationElementCollection.cs
public class AutomationElementCollectionEnumerator : IEnumerator
{
    private UIAutomationClient.IUIAutomationElementArray _obj;
    private int _index = -1; // Modify this line
    private int _cElem;
 
    #region IEnumerator Members
 
    internal AutomationElementCollectionEnumerator(UIAutomationClient.IUIAutomationElementArray obj)
    {
        Debug.Assert(obj != null);
        this._obj = obj;
        this._cElem = obj.Length;
    }
 
    public object Current
    {
        get
        {
            return AutomationElement.Wrap(this._obj.GetElement(this._index));
        }
    }
 
    public bool MoveNext()
    {
        if (this._index < (this._cElem - 1))
        {
            ++this._index;
            return true;
        }
        else
        {
            return false;
        }
    }
 
    public void Reset()
    {
        this._index = -1; // Modify this line
    }
 
    #endregion
}
}

file attachments

Closed Apr 2, 2012 at 5:39 AM by MSBernstein
This is fixed in the changelist I posted today.

comments