C# 有时我的listBox1或我更新的值会闪烁一秒或更短时间,我如何修复它?
这是我正在更新列表框的代码: 我还有两个列表框事件:C# 有时我的listBox1或我更新的值会闪烁一秒或更短时间,我如何修复它?,c#,winforms,C#,Winforms,这是我正在更新列表框的代码: 我还有两个列表框事件: private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e) { e.ItemHeight = 25; } private void listBox1_DrawItem(object sender, DrawItemEventArgs e) { if (e.Index == -1) { } else {
private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = 25;
}
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index == -1)
{
}
else
{
ColorText.ColorListBox(data, e);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace GatherLinks
{
class ColorText
{
public static void Texts(RichTextBox box, string text, Color color)
{
box.SelectionStart = box.TextLength;
box.SelectionLength = 0;
box.SelectionColor = color;
box.AppendText(text);
box.SelectionColor = box.ForeColor;
}
public static void ColorListBox(List<string> data, DrawItemEventArgs e)
{
string strLeft = null;
string strMid = "---";
string strRight = null;
if (data[e.Index].Contains(strMid))
{
int index = data[e.Index].IndexOf(strMid);
strLeft = data[e.Index].Substring(0, index);
strRight = data[e.Index].Substring(index + strMid.Length);
}
using (Font f = new Font(FontFamily.GenericSansSerif, 20, FontStyle.Regular))
{
float startPos;
e.Graphics.DrawString(strLeft, f, Brushes.Red, e.Bounds.X, e.Bounds.Y);
startPos = e.Graphics.MeasureString(strLeft, f).Width;
e.Graphics.DrawString(strMid, f, Brushes.Black, e.Bounds.X + startPos, e.Bounds.Y);
startPos = e.Graphics.MeasureString(strLeft + strMid, f).Width;
e.Graphics.DrawString(strRight, f, Brushes.Green, e.Bounds.X + startPos, e.Bounds.Y);
}
}
}
}
和另一个类中的ColorText函数,该函数为列表框中的项目着色:
private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = 25;
}
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index == -1)
{
}
else
{
ColorText.ColorListBox(data, e);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace GatherLinks
{
class ColorText
{
public static void Texts(RichTextBox box, string text, Color color)
{
box.SelectionStart = box.TextLength;
box.SelectionLength = 0;
box.SelectionColor = color;
box.AppendText(text);
box.SelectionColor = box.ForeColor;
}
public static void ColorListBox(List<string> data, DrawItemEventArgs e)
{
string strLeft = null;
string strMid = "---";
string strRight = null;
if (data[e.Index].Contains(strMid))
{
int index = data[e.Index].IndexOf(strMid);
strLeft = data[e.Index].Substring(0, index);
strRight = data[e.Index].Substring(index + strMid.Length);
}
using (Font f = new Font(FontFamily.GenericSansSerif, 20, FontStyle.Regular))
{
float startPos;
e.Graphics.DrawString(strLeft, f, Brushes.Red, e.Bounds.X, e.Bounds.Y);
startPos = e.Graphics.MeasureString(strLeft, f).Width;
e.Graphics.DrawString(strMid, f, Brushes.Black, e.Bounds.X + startPos, e.Bounds.Y);
startPos = e.Graphics.MeasureString(strLeft + strMid, f).Width;
e.Graphics.DrawString(strRight, f, Brushes.Green, e.Bounds.X + startPos, e.Bounds.Y);
}
}
}
}
如何使列表框不会每隔几秒钟或每次更新新传感器时闪烁。值?一、 我不确定具体发生的原因和时间,但ListBox项和sensor.Value正在闪烁。我认为问题在于您正在清除数据源,然后再次设置它。不必将数据源设置为null,然后在数据更改时将其重置 尝试删除这两行,看看闪烁是否消失:
this.Invoke(new Action(() => listBox1.DataSource = null));
this.Invoke(new Action(() => listBox1.DataSource = data));
您对Invoke方法的使用是错误的,这可能是闪烁的原因
尝试如下更改代码的第一部分:
this.Invoke(new Action(() =>
{
data = new List<string>();
data.Add("Gpu Temeprature --- " + sensor.Value.ToString());
listBox1.DataSource = null;
listBox1.DataSource = data;
listBox1.Invalidate()
}));
每次调用Invoke方法时,都会将消息发布到应用程序的消息队列,然后在UI不忙时在UI线程中处理消息。通过多次调用Invoke,您可以发送多条消息,这些消息将分别处理。一旦我解决了这个问题,请将其添加到我的表单中:
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
return cp;
}
}