Why is this gap showing up in my ListView?
I have a C# WinForm application where I am using a ListView to show what files have been uploaded to my database. I use the same code each time, calling LoadFileAttachments()
when the form loads, and again any time I refresh the list, or add or remove additional attachments from the database. (This part works great)
Where I am having an issue is the GUI side of the ListView. The first time LoadFileAttachments()
runs and fills the ListView, there is a gap between the left side of the ListView and the attachments. On subsequent calls, the gap disappears.
As you can see below, the columns are not changing width, there just seems to be a gap. I tried capturing the MouseClick event of the ListView and using a ListViewHitTestInfo to see what was there, and it is showing the item I am clicking next to, with the property of "Selected = false". Clicking on the icon or text causes the item to be selected, but not in the gap.
What's causing the gap?
Screenshot:
Screenshot of the gap/no gap http://img831.imageshack.us/img831/4054/fileattachments.png
The code I call each time:
private void LoadFileAttachments()
{
attachmentListView.Items.Clear();
ImageList iconList = new ImageList();
attachmentListView.LargeImageList = iconList;
attachmentListView.SmallImageList = iconList;
attachmentListView.StateImageList = iconList;
FileAttachmentInfo[] fileAttach = dbAccess.RetrieveAttachedRecords(loadPNGid.Value);
foreach (FileAttachmentInfo file in fileAttach)
{
ListViewItem item = new ListViewItem(file.FileName);
item.Tag = file.RowID;
iconList.Images.Add(file.FileExtention, ExtractIcons.GetIconImage(file.FileExtention));
item.ImageKey = file.FileExtention;
item.SubItems.Add(GetFileTypeDescriptors.GetFileDescriptor(file.FileExtention));
item.SubItems.Add(Conversions.FileSizeToString(file.FileSize));
item.SubItems.Add(file.DateAdded.ToShortDateString());
attachmentListView.Items.Add(item);
}
if (attachmentListView.Columns.Count == 0)
{
attachmentListView.Columns.Add("Attachment", 150);
attachmentListView.Columns.Add("File type", -2);
attachmentListView.Columns.Add("Size", -2);
attachmentListView.Columns.Add("Date added", -2);
}
}
This is the code in the designer file:
//
// attachmentListView
//
this.attachmentListView.Allow开发者_开发问答ColumnReorder = true;
this.attachmentListView.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.attachmentListView.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
this.attachmentListView.Location = new System.Drawing.Point(0, 0);
this.attachmentListView.MultiSelect = false;
this.attachmentListView.Name = "attachmentListView";
this.attachmentListView.Size = new System.Drawing.Size(440, 301);
this.attachmentListView.TabIndex = 0;
this.attachmentListView.TileSize = new System.Drawing.Size(188, 130);
this.attachmentListView.UseCompatibleStateImageBehavior = false;
this.attachmentListView.View = System.Windows.Forms.View.Details;
this.attachmentListView.DoubleClick += new System.EventHandler(this.attachmentListView_DoubleClick);
this.attachmentListView.MouseClick += new System.Windows.Forms.MouseEventHandler(this.attachmentListView_MouseClick);
I believe the problem is being caused by your setting of the StateImageList
property. According to the ListView.StateImageList documentation the StateImageList
is an additional image list that is displayed along side the SmallImageList
.
The
StateImageList
property allows you to specify an ImageList that contains images to use to represent an application-specific state of an item in a ListView control. State images are displayed to the left of an icon for the item. You can use state images, such as checked and unchecked check boxes, to indicate application-defined item states. State images are visible in all views of the ListView control.
Try commenting it out and see if that fixes your problem.
Does it make a different if you explicitly set the IndentCount
on the ListViewItem
to 0 when you're creating them?
New Answer based on investigation
I think this has to do with the ImageList
. In the designer, I can get similar behavior by adding and removing the image list. It doesn't get resolved until the ListView
is reconstructed.
I would add an ImageList
to the ListView
at the beginning and then simply clear and hydrate the same ImageList
repeatedly.
Would it make a difference if you specified the horizontal alignment?
attachmentListView.Columns.Add("Name", -2, HorizontalAlignment.Left);
精彩评论