0
点赞
收藏
分享

微信扫一扫

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

//}
}
}

龙腾一族至尊龙骑

举报

相关推荐

0 条评论