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

Article translations Article translations
Article ID: 813571 - View products that this article applies to.
This article has been archived. It is offered "as is" and will no longer be updated.
Expand all | Collapse all

On This Page

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.

Sample Code

Visual C# .NET

   this.listView1.Items.Clear();
   this.listView1.LargeImageList = this.largeImg;
   this.listView1.Items.Add(new ListViewItem("Item 1"));

Visual Basic .NET

   Me.listView1.Items.Clear()
   Me.listView1.LargeImageList = Me.largeImg
   listView1.Items.Add(New ListViewItem("Item 1"))

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Microsoft Visual Studio .NET (2003).
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, select either Visual C# Projects or Visual Basic Projects, and then select Smart Device Application under Templates.
  4. Name the project MyTestSmartDeviceApplication, and then click OK.
  5. Under Platform, select either Pocket PC or Windows CE, and under Project Type, select Windows Application, and then click OK.
  6. 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("Item " + 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 = "Set ImageList";
    		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 = "Form1";
    	}
    
    	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("Item " & 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 = "Set ImageList"
            '
            'Form1
            '
            Me.ClientSize = New System.Drawing.Size(240, 320)
            Me.Controls.Add(Me.listView1)
            Me.Controls.Add(Me.button1)
            Me.Text = "Form1"
    
        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
    
  7. On the Debug menu, click Start.
  8. Select either Pocket PC 2002 Emulator or Windows CE .NET Emulator, and then click Deploy.
  9. In Emulator, click Set ImageList. Notice the behavior that is described in the "Symptoms" section of this article.
  10. Quit the application.
  11. 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("Item " + 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("Item " & i))
                listView1.Items(i).ImageIndex = 0
            Next
    
        End Sub
  12. On the Debug menu, click Start.
  13. Select either Pocket PC 2002 Emulator or Windows CE .NET Emulator, and then click Deploy.
  14. In Emulator, click Set ImageList. Notice that the images are in serial order as expected.

REFERENCES

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
813562 PRB: Deployment on an Emulator Does Not Respond When You Select the COM1 Port in the Configure Emulator Settings Window

Properties

Article ID: 813571 - Last Review: February 27, 2014 - Revision: 2.4
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
Keywords: 
kbnosurvey kbarchive kblistview kbcontrol kbproperties kblayout kbprb KB813571

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com