zoukankan      html  css  js  c++  java
  • C# listView subitem 问本值 text 改变 界面会闪烁

    解决方法 就是重写ListView,然后设置双缓冲即可,然后再使用DoubleBufferListView,就不会闪烁了。下面的代码是DoubleBufferListView,并使用FrmMain来测试效果。

    代码如下

    第一步:DoubleBufferListView

    public class DoubleBufferListView : ListView
    {
    public DoubleBufferListView()
    {
    SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
    UpdateStyles();
    }
    }

    第二步

    新建测试窗体FrmMain ,并使用DoubleBufferListView

    public FrmMain()
    {
    InitializeComponent();
    CheckForIllegalCrossThreadCalls = false;
    ListView doubleBufferListView1 = new ListView();
    //
    // doubleBufferListView1
    //
    doubleBufferListView1.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
    doubleBufferListView1.FullRowSelect = true;
    doubleBufferListView1.HideSelection = false;
    doubleBufferListView1.Location = new System.Drawing.Point(50, 37);
    doubleBufferListView1.Name = "doubleBufferListView1";
    doubleBufferListView1.Size = new System.Drawing.Size(400, 191);
    doubleBufferListView1.TabIndex = 2;
    doubleBufferListView1.UseCompatibleStateImageBehavior = false;
    doubleBufferListView1.View = System.Windows.Forms.View.Details;
    this.pnlflashing.Controls.Add(doubleBufferListView1);
    doubleBufferListView1.Clear();
    doubleBufferListView1.Columns.Add("Action", 80, System.Windows.Forms.HorizontalAlignment.Left);
    doubleBufferListView1.Columns.Add("value", 80, System.Windows.Forms.HorizontalAlignment.Right);
    doubleBufferListView1.Columns.Add("Action", 80, System.Windows.Forms.HorizontalAlignment.Left);
    doubleBufferListView1.Columns.Add("value", 80, System.Windows.Forms.HorizontalAlignment.Left);
    string[] listViewData = new string[4];
    listViewData[0] = "Action";
    listViewData[1] = "1";
    listViewData[2] = "Action";
    listViewData[3] = "1";
    ListViewItem lvItem = new ListViewItem(listViewData, 0);
    doubleBufferListView1.Items.Add(lvItem);
    }

    第三步:按钮事件

    bool state;
    private void button1_Click_1(object sender, EventArgs e)
    {
    Thread th = new Thread(PlayGame);
    th.IsBackground = true;
    if (state == false)
    {
    state = true;
    button1.Text = "停止";

    th.Name = "新线程";
    th.Start();
    }
    else
    {
    state = false;
    button1.Text = "开始";

    }
    }
    private void PlayGame()
    {
    //try
    //{
    Random r = new Random();
    while (state)
    {
    if (IsHandleCreated)
    {

    //Invoke跨线程调用,MethodInvoker  这个内部分委托,非常方便。亲测,其他委托关闭主窗体时会报错。
    this.Invoke((MethodInvoker)delegate ()
    {
    string temp = r.Next(0, 10).ToString();
    label1.Text = temp;
    ((ListView)pnlflashing.Controls["doubleBufferListView1"]).Items[0].SubItems[1].Text = temp;
    Application.DoEvents();
    });
    }
    //if (this.IsHandleCreated)
    //{
    // string temp = r.Next(0, 10).ToString();
    // //label1.Text = temp;
    // this.Invoke(new MethodInvoker(() =>
    // {
    // label1.Text = temp;
    // ((ListView)pnlflashing.Controls["doubleBufferListView1"]).Items[0].SubItems[1].Text = temp;
    // }));
    //}
    }

    //}
    //catch
    //{

    //}
    }
    }

  • 相关阅读:
    (004)maven执行junit单元测试,指定执行哪些测试类
    (009)Nginx静态资源web服务
    (008)Nginx的访问控制_介绍实现访问控制的基本方式
    (03)Nginx将配置文件default.conf重命名后报Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.解决方案
    (007)Nginx的请求限制_配置语法与原理
    (006)Nginx之模块讲解
    (005)Nginx之日志log_format
    (004)Nginx默认配置语法解析及演示
    (003)Nginx编译配置参数讲解
    Gym
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/8692221.html
Copyright © 2011-2022 走看看