C# WinForm: Prevent listView from repainting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • azoapes
    New Member
    • Apr 2008
    • 7

    C# WinForm: Prevent listView from repainting

    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.Visibl e = false; but it doesn't speed it up, i.e. the listView still repaints itself).

    There are the listView.BeginU pdate() 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
  • shinevpaul
    New Member
    • Mar 2008
    • 7

    #2
    Hi Simeon

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

    Comment

    • azoapes
      New Member
      • Apr 2008
      • 7

      #3
      Originally posted by shinevpaul
      Hi Simeon

      Check the condition if(!page.IsPost Back) 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.

      Comment

      • azoapes
        New Member
        • Apr 2008
        • 7

        #4
        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...

        Comment

        • azoapes
          New Member
          • Apr 2008
          • 7

          #5
          *bump*

          No one is able to help?

          Comment

          • brockweaver
            New Member
            • Jun 2008
            • 3

            #6


            I wrap mine in a class that implements IDisposable so I can using() them...
            Code:
            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;
            using System.Runtime.InteropServices;
            using System.Windows.Forms;
            
            namespace WindowsApplication1.Admin {
                public class FreezeWindow : IDisposable {
            
                    public FreezeWindow(IntPtr ptr) {
                        LockWindowUpdate(ptr);
                    }
            
                    [DllImport("user32.dll")]
                    static extern bool LockWindowUpdate(IntPtr hWndLock);
            
                    public void Dispose() {
                        LockWindowUpdate(IntPtr.Zero);
                    }
                }
            }
            ... then in the code for whatever form / control / etc you want to freeze...
            Code:
            using (new FreezeWindow(this.Handle)) {
              // do lots of stuff there
            }
            Last edited by Frinavale; Sep 15 '09, 06:48 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

            Comment

            • brockweaver
              New Member
              • Jun 2008
              • 3

              #7
              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():
              Code:
              var lvis = new List<ListViewItem>();
              lvis.Add(new ListViewItem("hi1"));
              lvis.Add(new ListViewItem("hi2"));
              // etc
              
              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.
              Last edited by Frinavale; Sep 15 '09, 06:48 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                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:
                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?

                Comment

                Working...