views:

58

answers:

3

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

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.AllowColumnReorder = 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);
A: 

Would it make a difference if you specified the horizontal alignment?

attachmentListView.Columns.Add("Name", -2, HorizontalAlignment.Left);
DOK
Just tried it out, and no change. I made a mistake in my question, though - the hit test when I click on the gap DOES show the item I'm clicking next to. Editing the question now...
Jared Harley
+1  A: 

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.

Jacob G
Interesting idea. There is no effect when I set it explicitly to zero, but there IS a difference when I set it to 1. When I set `IndentCount` on each item to 1, the first time the list loads, it's where the items would be if I set the indent to 2. After a refresh, it moves to where Indent=1 would be, which is how the list looks when it first loads. It's like there's a disappearing Indent on the items in the list!
Jared Harley
Can we see the initial settings of the ListView? Where attachmentListView is instantiated... (not thinking it'll make a difference, but just in case.
Jacob G
@Jacob added it to the bottom of my post
Jared Harley
+2  A: 

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.

Joshua
That did it! I'm not sure why the problem was going away after running the code twice (since the StateImageList was getting built twice) but this prevents it from happening in the first place. Thanks!
Jared Harley