private ListViewItem GetItem(int index) { //information needed for parsing var fileName = _filesArray[index].Name; var fileType = Methods.GetLegoFileType(fileName); //icon assignation (default is the 'Unknown File' icon) var imageIndex = FileIcon.FileIconIndex(fileName); //item to add to the list view var newItem = new ListViewItem(@"", imageIndex); //construct sub-items var items = new[] { new ListViewItem.ListViewSubItem(newItem, index.ToString()), new ListViewItem.ListViewSubItem(newItem, fileName), new ListViewItem.ListViewSubItem(newItem, fileType), new ListViewItem.ListViewSubItem(newItem, $"0x{_filesArray[index].Crc:X8}"), new ListViewItem.ListViewSubItem(newItem, $"0x{_filesArray[index].Offset:X8}"), new ListViewItem.ListViewSubItem(newItem, _filesArray[index].SizeUnComp.ToString()), new ListViewItem.ListViewSubItem(newItem, _filesArray[index].Size.ToString()), new ListViewItem.ListViewSubItem(newItem, $"0x{_filesArray[index].Pack:X3}") }; //add sub-items newItem.SubItems.AddRange(items); //return the final item return(newItem); }
private void PopulateListView() { //clear the list of any items then disable it to prevent user input while working lstMain.Invoke((MethodInvoker) delegate { lstMain.Items.Clear(); lstMain.Enabled = false; }); var nodeDirInfo = (DirectoryInfo)_newSelected.Tag; ListViewItem item; //reset progress bar lstMain.Invoke((MethodInvoker) delegate { if (pbMain.ProgressBar == null) { return; } pbMain.ProgressBar.Maximum = nodeDirInfo.GetFiles().Length; pbMain.ProgressBar.Value = 0; }); foreach (var file in nodeDirInfo.GetFiles()) { //index 1 is the 'Unknown File' icon var imageIndex = FileIcon.FileIconIndex(file.Name); //the new item to add item = new ListViewItem(file.Name, imageIndex); //the cells that belong to the new item var items = new[] { new ListViewItem.ListViewSubItem(item, Methods.GetLegoFileType(file.Extension)), new ListViewItem.ListViewSubItem(item, Methods.FormatSize(file.Length, true)), new ListViewItem.ListViewSubItem(item, file.LastAccessTime.ToShortDateString()) }; //add the new sub items item.SubItems.AddRange(items); //invoke and begin adding lstMain.Invoke((MethodInvoker) delegate { //new entry is added lstMain.Items.Add(item); //the method is exited if the progress bar is undefined if (pbMain.ProgressBar == null) { return; } //the progress bar is incremented ++pbMain.ProgressBar.Value; //percentage completion calculation var percComplete = (int)(pbMain.ProgressBar.Value / (double)pbMain.ProgressBar.Maximum * 100.0); //draws the % completion inside of the progressbar (on-the-fly) pbMain.ProgressBar.CreateGraphics().DrawString($"{percComplete}%", new Font("Arial", 8.25f, FontStyle.Regular), Brushes.Gray, new PointF(pbMain.ProgressBar.Width / 2 - 10, pbMain.ProgressBar.Height / 2 - 7)); }); } //resize the columns to fit the window lstMain.Invoke((MethodInvoker) delegate { lstMain.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent); lstMain.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize); lstMain.Enabled = true; }); }