我用计时器更新Listview时会闪烁 [英] Listview flickers when I update it with a timer

查看:64
本文介绍了我用计时器更新Listview时会闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个计时器(1秒刷新)更新我的部分或全部listview项目和子项的内容,但是listview每秒钟闪烁一次.有时在重新绘制过程中会丢失子项.因为我的列表视图包含随时可能更改的数据,所以我使用了计时器.

I would like to update some or all of my listview's items and subitems contents with a timer (1 second refresh) But the listview flicker each one second. Sometimes the subitems are lost during redrawing. Because my listview contains data that is to be likely changed anytime, I use a timer.

代码:我将此功能放在了计时器的Tick方法中

Code: I put this function in the timer's Tick method

void Refresh()
{
   foreach(string s in lsttring)
   {
      lv.items.add(s);
      lv.items[i].subitems.add(i);
   }
}

我希望只更改已更改的项目内容(项目文本和子项目文本),而不更改整个列表视图以及计时器刻度.

I expect only items content (item text and subitem text) that are changed will be changed not the whole listview along with the timer tick.

推荐答案

ListView控件支持双缓冲,它将DoubleBuffered属性映射到本机控件的LVS_EX_DOUBLEBUFFER样式标志.它非常有效,但是由于它是受保护的属性,因此您无法直接使用它.在您的项目中添加一个新类,并粘贴以下代码.编译.将新控件从工具箱的顶部拖放到您的窗体上,以替换旧控件.

The ListView control supports double buffering, it maps the DoubleBuffered property to the native control's LVS_EX_DOUBLEBUFFER style flag. It is quite effective but you can't get to it directly since it is a protected property. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form, replacing the old one.

using System;
using System.Windows.Forms;

class BufferedListView : ListView {
    public BufferedListView() {
        this.DoubleBuffered = true;
    }
}

这篇关于我用计时器更新Listview时会闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆