private void populateQbList(QbItemBase selectItem)
{
ListViewItem topItem = null;
try
{
bool isRefresh = (selectItem != null);
lstQbItems.BeginUpdate();
//try to preserve the top item
ListViewItem top = lstQbItems.TopItem; //can return null
int topIdx = 0;
//if is refresh, try and select the same item
if (isRefresh && top != null)
{
for (int i = 0; i < lstQbItems.Items.Count; i++)
{
if (lstQbItems.Items[i] == top)
{
topIdx = i;
break;
}
}
}
//clear items without as much flicker as Items.Clear
for (int i = lstQbItems.Items.Count - 1; i >= 0; i--)
lstQbItems.Items.RemoveAt(i);
foreach (QbItemBase itm in _qbFile.Items)
{
addItemToGui(itm, 0);
addSubItemsToGui(itm, 0 + 1);
}
ListViewItem foundItem = null;
if (selectItem != null)
{
foreach (ListViewItem li in lstQbItems.Items)
{
if (li.Tag == selectItem)
{
foundItem = li;
break;
}
}
}
if (foundItem == null && lstQbItems.Items.Count != 0)
foundItem = lstQbItems.Items[0];
if (isRefresh && top != null && lstQbItems.Items.Count != 0 && topIdx >= 0 && topIdx < lstQbItems.Items.Count)
{
topItem = lstQbItems.Items[topIdx];
lstQbItems.TopItem = topItem;
lstQbItems.TopItem = topItem; //crazy, but stops the wrong item being set.
}
if (foundItem != null)
{
foundItem.Selected = true;
foundItem.Focused = true;
foundItem.EnsureVisible();
}
if (lstQbItems.SelectedIndices.Count == 0 && lstQbItems.Items.Count != 0)
{
lstQbItems.Items[0].Selected = true;
lstQbItems.Items[0].Focused = true;
}
if (lstQbItems.SelectedIndices.Count == 0)
{
gboEdit.Controls.Clear();
}
tlblQbFileInfo.Text = string.Format("QB file: {0} items, {1} bytes", lstQbItems.Items.Count.ToString(), _qbFile.Length.ToString());
}
catch (Exception ex)
{
showException("QB List Population Error", ex);
clearInterfaceQb();
return;
}
finally
{
lstQbItems.EndUpdate();
}
}