Elan
Elan

Reputation: 6376

How set columns to fixed size in Winforms ListView with view style View.SmallIcon

I have a C# Winforms application and am using the ListView control. When I set the View property to View.SmallIcon the icons are not aligned into fixed size columns. The items are spread all over the place.

Is there a property to set on the ListView control to cause fixed sized columns in the SmallIcon view.

Update:
Below are screenshots depicting the problem I am running into. Screenshot #1 shows Windows Explorer where file names are fixed in width and ellipsis are introduced. While technically there may not be any actual columns in play, the icons are aligned in "columnar" type layout. Image
(source: barramsoft.com)

Screenshot #2 shows what I am running into. The icons have a not so pretty layout: Image
(source: barramsoft.com)

Upvotes: 3

Views: 7096

Answers (4)

m.zam
m.zam

Reputation: 467

I solve this by controlling the text length as below :

   int max_length = 30; // you can use any value here

   ListViewItem l = lsvLocalFile.Items.Add("some text value");
   l.ImageIndex = index_of_smallimagelist;
   if (l.Text.Length > max_length) 
       l.Text = l.Text.Remove(max_length - 3) + "...";
   else
       l.Text = l.Text.PadRight(max_length);

Upvotes: 1

user1399165
user1399165

Reputation: 116

I had this problem too. I solved it by adding a column to the ListView using fileNamesListView.Columns.Add(new ColumnHeader()); and then setting fileNamesListView.Columns[0].Width to the width of the longest item text and its image. The disadvantage is that all columns are the width of the widest column.

private void PopulateListView(List<string> fileNames)
{
    using (Graphics g = this.CreateGraphics())
    {
        int longestTextWidth = 0;
        int longestTextIndex = 0;

        for (int i = 0; i < fileNames.Count; i++)
        {
            ListViewItem item = new ListViewItem(fileNames[i]);
            item.ImageIndex = 0; // Do whatever you do to choose the image.
            fileNamesListView.Items.Add(item);

            // Find the longest file name.
            int textWidth = Size.Round(g.MeasureString(fileNames[i], fileNamesListView.Font)).Width;
            if (textWidth > longestTextWidth)
            {
                longestTextWidth = textWidth;
                longestTextIndex = i;
            }
        }

        // Find the width of the image used.
        int imageWidth = filesImageList.Images[fileNamesListView.Items[longestTextIndex].ImageIndex].Width;

        fileNamesListView.Columns[0].Width = longestTextWidth + imageWidth;
    }
}

Upvotes: 1

Libor
Libor

Reputation: 3303

You can try freeware Better ListView Express component, it shows items in SmallIcon view always aligned and exactly like the Windows Explorer does.

You can also display column headers in all views (not only Details), which is not possible in .NET ListView.

ComponentOwl also offers a full version with many extra features.

Upvotes: 0

Cody Gray
Cody Gray

Reputation: 244981

The only view that uses columns is the "Details" view.

The "Small Icon" view is the same as the "Large Icon" view, except with smaller icons. The icons can be positioned all over the control. You can play with this for yourself in Windows Explorer by changing the view; it uses a ListView control.

If you want to force the icons to line up in nice little rows, combine the Alignment property with the AutoArrange property.

Set Alignment to either ListViewAlignment.Top or ListViewAlignment.Left, and AutoArrange to true to keep the icons automatically arranged in this position.

You can do this either in the designer or via code:

myListView.Alignment   = ListViewAlignment.Top;
myListView.AutoArrange = true;

Upvotes: 1

Related Questions