internal static void BuildListviewControl(List <AutomationElement> childElements, LAPListViewControl lapListViewObjects, EventHandler linkItemClick ) { int[] checkedIndexes = lapListViewObjects.CheckedIndicesArray; ListView.SelectedIndexCollection selectedIndexs = lapListViewObjects.SelectedIndices; int selectIndex = selectedIndexs.Count == 0 ? 0 : selectedIndexs[0]; lapListViewObjects.BeginUpdate(); foreach (Control control in lapListViewObjects.Controls) { if (control.Name.IndexOf("linkLabel") >= 0) { control.Click -= linkItemClick;// lvItem_lnLinkItemClick; control.Dispose(); } } lapListViewObjects.Items.Clear(); if (0 == childElements.Count) { lapListViewObjects.EndUpdate(); return; } int index = 0; int i = 0, j = 0; ElementNotAvailableException elementNotAvailableException = null; List <ElementProperties> propertiesList = null; Utility.AsyncCall(() => { try { propertiesList = childElements.ConvertAll <ElementProperties>((element) => { System.Windows.Rect rect = element.Cached.BoundingRectangle; if (rect.IsEmpty) { return(null); } return(new ElementProperties(element)); }); } catch (ElementNotAvailableException ex) { lapListViewObjects.Invoke(new Action(() => { lapListViewObjects.EndUpdate(); })); //TODO, still not being handled by some handler elementNotAvailableException = ex; } }); if (elementNotAvailableException != null) { lapListViewObjects.EndUpdate(); throw elementNotAvailableException; } foreach (var properties in propertiesList) { //zero size control will be skipped. if (properties == null) { continue; } ListViewSubItem[] lvSubItem = new ListViewSubItem[3]; Graphics graphics = lapListViewObjects.CreateGraphics(); string controlType = properties.ControlType.ControlTypeToString(); LinkLabel linkLabel = new LinkLabel(); linkLabel.Name = string.Format("linkLabel{0}", j++); linkLabel.Text = controlType; linkLabel.AutoSize = true; linkLabel.Click += linkItemClick; //lvItem_lnLinkItemClick; lvSubItem[0] = new ListViewSubItem() { Text = string.Empty }; lvSubItem[1] = new ListViewSubItem() { Text = controlType, Tag = linkLabel }; lvSubItem[2] = new ListViewSubItem() { Text = properties.Name }; graphics.Dispose(); ListViewItem lvItem = new ListViewItem(lvSubItem, 0); if (checkedIndexes.Length > 0 && i < checkedIndexes.Length) { if (index == checkedIndexes[i]) { lvItem.Checked = true; ++i; } } // lvItem.Checked = true; lvItem.Tag = properties; lapListViewObjects.Items.Add(lvItem); ++index; } if (lapListViewObjects.Items.Count > 0) { lapListViewObjects.Focus(); ListViewItem lvItem = selectIndex >= lapListViewObjects.Items.Count ? lapListViewObjects.Items[0] : lapListViewObjects.Items[selectIndex]; lvItem.Selected = true; lvItem.Focused = true; } lapListViewObjects.EndUpdate(); }