Microsoft KB Archive/813571

= PRB: Setting the LargeImageList Property of the ListView Control at Runtime Causes Rearrangement of ListView Items =

Article ID: 813571

Article Last Modified on 5/20/2005

-

APPLIES TO


 * Microsoft .NET Compact Framework
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition

-



SYMPTOMS
When you set the LargeImageList property of the ListView control to an existing ImageList during runtime, the items in the ListView control may become rearranged.



WORKAROUND
To work around this problem, set the LargeImageList property to an ImageList before you add the items to ListView.

If you want to change the LargeImageList property to another ImageList, follow these steps:
 * 1) Remove all the items in the ListView control.
 * 2) Assign ImageList to the LargeImageList property.
 * 3) Repopulate ListView.

Visual C# .NET
this.listView1.Items.Clear; this.listView1.LargeImageList = this.largeImg; this.listView1.Items.Add(new ListViewItem(&quot;Item 1&quot;));

Visual Basic .NET
Me.listView1.Items.Clear Me.listView1.LargeImageList = Me.largeImg listView1.Items.Add(New ListViewItem(&quot;Item 1&quot;))



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
 Start Microsoft Visual Studio .NET (2003). On the File menu, point to New, and then click Project. Under Project Types, select either Visual C# Projects or Visual Basic Projects, and then select Smart Device Application under Templates. Name the project MyTestSmartDeviceApplication, and then click OK. Under Platform, select either Pocket PC or Windows CE, and under Project Type, select Windows Application, and then click OK.  Replace the existing code with the following code that is appropriate for your application:

Visual C# .NET Sample Code
using System; using System.Drawing; using System.Drawing.Imaging; using System.Collections; using System.Windows.Forms;

public class Form1 : System.Windows.Forms.Form {   private Button button1; private ListView listView1; private ImageList largeImg;

public Form1 {       InitializeComponent;

Bitmap bmp = GetBitmap(32, 32); largeImg = new ImageList; largeImg.ImageSize = new Size(bmp.Width, bmp.Height); largeImg.Images.Add(bmp); for(int i = 0; i < 7; i++) {           this.listView1.Items.Add(new ListViewItem(&quot;Item &quot; + i)); this.listView1.Items[i].ImageIndex = 0; }   }    private void InitializeComponent {       this.button1 = new System.Windows.Forms.Button; this.listView1 = new System.Windows.Forms.ListView; //        // button1 //        this.button1.Location = new System.Drawing.Point(68, 240); this.button1.Size = new System.Drawing.Size(104, 20); this.button1.Text = &quot;Set ImageList&quot;; this.button1.Click += new System.EventHandler(this.button1_Click); //        // Form1 //        this.ClientSize = new System.Drawing.Size(240, 320); this.Controls.Add(this.button1); this.Controls.Add(this.listView1); this.Text = &quot;Form1&quot;; }

static void Main {       Application.Run(new Form1); }

private void button1_Click(object sender, System.EventArgs e)   { this.listView1.LargeImageList = this.largeImg; }

protected Bitmap GetBitmap(int w, int h)   { Bitmap b = new Bitmap(w, h); Graphics g = Graphics.FromImage(b);

g.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(0, 0, b.Width, b.Height)); g.Dispose;

return b;   }

protected override void OnResize(EventArgs e)   { base.OnResize(e); this._Layout; }

protected void _Layout {       this.button1.Bounds = new Rectangle((this.ClientSize.Width - this.button1.Width) / 2, this.ClientSize.Height - this.button1.Height, this.button1.Width, this.button1.Height); this.listView1.Bounds = new Rectangle(0, 0, this.ClientSize.Width, this.button1.Top); } }

Visual Basic .NET Sample Code
Imports System Imports System.Drawing Imports System.Drawing.Imaging Imports System.Collections Imports System.Windows.Forms

Public Class Form1 Inherits System.Windows.Forms.Form

Private WithEvents button1 As Button Private listView1 As ListView Private largeImg As ImageList

Public Sub New InitializeComponent Dim bmp As Bitmap bmp = GetBitmap(32, 32) largeImg = New ImageList largeImg.ImageSize = New Size(bmp.Width, bmp.Height) largeImg.Images.Add(bmp) Dim i As Integer = 0 For i = 0 To 6 Me.listView1.Items.Add(New ListViewItem(&quot;Item &quot; & i.ToString)) Me.listView1.Items(i).ImageIndex = 0 Next i   End Sub

Private Sub InitializeComponent Me.button1 = New System.Windows.Forms.Button Me.listView1 = New System.Windows.Forms.ListView Me.listView1.Size = New System.Drawing.Size(200, 200) Me.listView1.Location = New System.Drawing.Point(20, 0) '       'button1 '       Me.button1.Location = New System.Drawing.Point(68, 240) Me.button1.Size = New System.Drawing.Size(104, 20) Me.button1.Text = &quot;Set ImageList&quot; '       'Form1 '       Me.ClientSize = New System.Drawing.Size(240, 320) Me.Controls.Add(Me.listView1) Me.Controls.Add(Me.button1) Me.Text = &quot;Form1&quot;

End Sub

Protected Function GetBitmap(ByVal width As Integer, ByVal height As Integer) As Bitmap Dim bmp As Bitmap bmp = New Bitmap(width, height) Dim graph As Graphics graph = Graphics.FromImage(bmp) graph.FillRectangle(New SolidBrush(Color.Gray), New Rectangle(0, 0, bmp.Width, bmp.Height)) graph.Dispose

Return bmp End Function Protected Overrides Sub OnResize(ByVal e As EventArgs) MyBase.OnResize(e) Me._Layout End Sub Protected Sub _Layout Me.button1.Bounds = New Rectangle((Me.ClientSize.Width - Me.button1.Width) / 2, Me.ClientSize.Height - Me.button1.Height, Me.button1.Width, Me.button1.Height) Me.listView1.Bounds = New Rectangle(0, 0, Me.ClientSize.Width, Me.button1.Top) End Sub

Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click Me.listView1.LargeImageList = Me.largeImg End Sub End Class </li> On the Debug menu, click Start.</li> Select either Pocket PC 2002 Emulator or Windows CE .NET Emulator, and then click Deploy.</li> In Emulator, click Set ImageList. Notice the behavior that is described in the &quot;Symptoms&quot; section of this article.</li> Quit the application.</li>  Replace the Button1_Click code with the following code that is appropriate for your application:

Visual C# .NET Sample Code
private void button1_Click(object sender, System.EventArgs e)   { this.listView1.Items.Clear; this.listView1.LargeImageList = this.largeImg; for(int i = 0; i < 7; i++) {           this.listView1.Items.Add(new ListViewItem(&quot;Item &quot; + i.ToString)); this.listView1.Items[i].ImageIndex = 0; }   }

Visual Basic .NET Sample Code
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click Me.listView1.Items.Clear Me.listView1.LargeImageList = Me.largeImg

Dim i As Int32 For i = 0 To 6 listView1.Items.Add(New ListViewItem(&quot;Item &quot; & i)) listView1.Items(i).ImageIndex = 0 Next

End Sub </li> On the Debug menu, click Start.</li> Select either Pocket PC 2002 Emulator or Windows CE .NET Emulator, and then click Deploy.</li> In Emulator, click Set ImageList. Notice that the images are in serial order as expected.</li></ol>

<div class="references_section">