Connecting Tech Pros Worldwide Help | Site Map

C# WinForm: Prevent listView from repainting

Newbie
 
Join Date: Apr 2008
Posts: 7
#1: Apr 2 '08
Hello, I'm trying to prevent a listView from repainting when I change group assignments for the items. Let's assume I have two groups with 100 items in each and moves 100 items from group A to group B, then the listView will repaint itself a lot, slowing down the operation and flickering (can be solved by listView.Visible = false; but it doesn't speed it up, i.e. the listView still repaints itself).

There are the listView.BeginUpdate() and EndUpdate() methods, but they don't seem to work for other actions than on the listView.Items collection (e.g. listView.Items.Add)

I can turn it into OwnerDraw = true; but that would mean a lot of work, I guess? This is both a visual and performance enhancement though, so maybe it's worth it, but maybe there's a much simpler solution?

Regards, Simeon
Newbie
 
Join Date: Mar 2008
Posts: 7
#2: Apr 2 '08

re: C# WinForm: Prevent listView from repainting


Hi Simeon

Check the condition if(!page.IsPostBack) before loading items to the listbox everytime.
Newbie
 
Join Date: Apr 2008
Posts: 7
#3: Apr 2 '08

re: C# WinForm: Prevent listView from repainting


Quote:

Originally Posted by shinevpaul

Hi Simeon

Check the condition if(!page.IsPostBack) before loading items to the listbox everytime.

I added "WinForm" to the title for a reason... this is a Windows App, not ASP.NET

Also, it's a listView... not listBox.
Newbie
 
Join Date: Apr 2008
Posts: 7
#4: Apr 2 '08

re: C# WinForm: Prevent listView from repainting


I just found a really old post with the exact same problem, about two and a half year ago. It's located here: http://bytes.com/forum/thread239168.html

Nobody ever solved that one...
Newbie
 
Join Date: Apr 2008
Posts: 7
#5: Apr 4 '08

re: C# WinForm: Prevent listView from repainting


*bump*

No one is able to help?
Newbie
 
Join Date: Jun 2008
Posts: 3
#6: Sep 15 '09

re: C# WinForm: Prevent listView from repainting


http://www.pinvoke.net/default.aspx/...dowUpdate.html

I wrap mine in a class that implements IDisposable so I can using() them...
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6. using System.Windows.Forms;
  7.  
  8. namespace WindowsApplication1.Admin {
  9.     public class FreezeWindow : IDisposable {
  10.  
  11.         public FreezeWindow(IntPtr ptr) {
  12.             LockWindowUpdate(ptr);
  13.         }
  14.  
  15.         [DllImport("user32.dll")]
  16.         static extern bool LockWindowUpdate(IntPtr hWndLock);
  17.  
  18.         public void Dispose() {
  19.             LockWindowUpdate(IntPtr.Zero);
  20.         }
  21.     }
  22. }
... then in the code for whatever form / control / etc you want to freeze...
Expand|Select|Wrap|Line Numbers
  1. using (new FreezeWindow(this.Handle)) {
  2.   // do lots of stuff there
  3. }
Newbie
 
Join Date: Jun 2008
Posts: 3
#7: Sep 15 '09

re: C# WinForm: Prevent listView from repainting


Sorry, the best way to fix your problem is to create a temporary list of ListViewItem objects, then add them to your list view with AddRange():
Expand|Select|Wrap|Line Numbers
  1. var lvis = new List<ListViewItem>();
  2. lvis.Add(new ListViewItem("hi1"));
  3. lvis.Add(new ListViewItem("hi2"));
  4. // etc
  5.  
  6. listView.Items.AddRange(lvis);
That way the control only has to update itself once. The API approach I gave before is good for generic case, but not the best solution for yours as this one doesn't resort to calling into the API but still does what you need.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#8: Sep 15 '09

re: C# WinForm: Prevent listView from repainting


What if you had a custom class that inherits from it, then override the paint() method.
Then just have a bool that determines if it calls base.Paint() or not?

Also, maybe this could be used to advantage:
Quote:

Originally Posted by msdn

Any groups assigned to a ListView control appear whenever the View property is set to a value other than View.List.

Sounds like setting it to View.List would cause it to ignore the groups, perhaps keeping them from being slow?
Reply