private void InsertLogMessageByDate(ListView.ListViewItemCollection items, LogMessageItem refItem) { // Get previous item LogMessageItem prevItem = refItem; while ((prevItem != null) && !prevItem.Enabled) prevItem = prevItem.Previous; int pos = 0; if ((prevItem != null) && (prevItem.Item != null)) pos = items.IndexOf(prevItem.Item) + 1; items.Insert(pos, refItem.Item); // Mark the item as enabled refItem.Enabled = true; }