C# listView subitem 问本值 text 改变 界面会闪烁

网友投稿 728 2022-11-24

C# listView subitem 问本值 text 改变 界面会闪烁

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 //{

//} } }

龙腾一族至尊龙骑

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:C# 写入XML文档三种方法详细介绍
下一篇:socket-WebSocket-HttpListener-TcpListener服务端客户端的具体使用案例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~