How C # resolves updates to the ListView control and UI flicker issues when updating

Source: Internet
Author: User

First question: How to update the contents of a ListView control

Many times when you run a form program, the following exception occurs when you manipulate a control by using multithreading in the program and improper operation:

This is because the control we add to the form has its own thread, so it cannot be accessed from other threads. How do we fix it? Use delegate:methodinvoker , I use this very convenient. Here is a use case:
1 //call Initlistview () to allow the ListView control to be freely updated.2 Private voidInitlistview ()3 {4MethodInvoker in =NewMethodInvoker (initlistviewed);5      This. BeginInvoke (in);6 }7 Private voidinitlistviewed ()8 {Ten     //in this case, you can build the ListView control and update the data. One listView1.Columns.Clear (); A}

Second question: How to troubleshoot a flashing issue when a ListView control is updated

When I update a control, I use a timed update, and every time the content of the control is updated, the user experience is not a good idea.

There is no way to make it not flash, my solution is to use double buffering .

Here is an example of a simple step and approach.

1, the first step: we create a class, the class name is listviewnf, is the ListView control that inherits the form

classListViewNF:System.Windows.Forms.ListView { Publiclistviewnf () {//turn on double buffering             This. SetStyle (Controlstyles.optimizeddoublebuffer | Controlstyles.allpaintinginwmpaint,true);  This. SetStyle (Controlstyles.enablenotifymessage,true); }        protected Override voidonnotifymessage (Message m) {//Filter out the WM_ERASEBKGND message            if(M.msg! =0x14)            {                Base.            Onnotifymessage (m); }        }    }

Note: The added references are added.

Step Two: Modify the code generated by the form designer

  The private System.Windows.Forms.ListViewNF ListView; Comment out or delete a line of code

  Add LISTVIEWNF ListView1;

LISTVIEWNF ListView1;
// private System.Windows.Forms.ListViewNF ListView;

The third step: since you changed the ListView to ListView1, there will be a lot of errors, so you need to change the location of the ListView to ListView1,

There is one place where you need to change the original code to a format (where the yellow box is circled)

In the above way, the content will be very comfortable to update.

I do not know that I am not clear, wrong or there is a better way for everyone to communicate with me, I also just use C #, still do not know.

How C # resolves updates to the ListView control and UI flicker issues when updating

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.