C # How to Solve the Problem of page flickering when updating and updating the ListView control,

Source: Internet
Author: User

C # How to Solve the Problem of page flickering when updating and updating the ListView control,

First question: how to update the content of the ListView Control

Many times, when you run a form program, the following exception occurs when you operate controls due to the use of multithreading and improper operations in the program:

This is because all the controls we add in the form have their own threads, so they cannot be accessed from other threads. What should we do? Delegate: MethodInvokerIt is convenient for me to use it. The following is an example:
1 // call InitListView () to freely update the ListView control. 2 private void InitListView () 3 {4 MethodInvoker In = new MethodInvoker (InitListViewed); 5 this. beginInvoke (In); 6} 7 private void InitListViewed () 8 {10 // you can build the ListView control and update the data here. 11 listView1.Columns. clear (); 12}

Second problem: how to solve the problem of flashing when the ListView control is updated

When I update a control, I use regular update. The content of each update control will flash, and the user experience is not good at all.

Is there a way to make it do not Flash? My solution is to useDouble Buffering.

The following is an example to illustrate the steps and methods.

1. Step 1: Create a class named ListViewNF, which inherits the ListView control of the form.

Class ListViewNF: System. windows. forms. listView {public ListViewNF () {// enable double buffering this. setStyle (ControlStyles. optimizedDoubleBuffer | ControlStyles. allPaintingInWmPaint, true); this. setStyle (ControlStyles. enableNotifyMessage, true);} protected override void OnNotifyMessage (Message m) {// Filter out the WM_ERASEBKGND message if (m. msg! = 0x14) {base. onpolicymessage (m );}}}

 

Note: All references to be added must be added.

Step 2: modify the code generated by the Form Designer

Comment out or delete a line of code in private System. Windows. Forms. ListViewNF listView.

Add ListViewNF listView1;

ListViewNF listView1;
//private System.Windows.Forms.ListViewNF listView;

Step 3: Because you change listView to listView1, a large number of errors will occur, so you need to change the location where listView is used to listView1,

Here, you need to change the original code to the format (the place circled by a yellow box)

Through the above method, the content will be very comfortable to update.

I don't know. I'm not clear. I have a mistake or have a better way to communicate with me. I just used C # And I don't know much about it.

 

Related Article

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.