Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# “有麻烦”;“闪烁”;更新面板时';孩子们_C#_.net_Winforms - Fatal编程技术网

C# “有麻烦”;“闪烁”;更新面板时';孩子们

C# “有麻烦”;“闪烁”;更新面板时';孩子们,c#,.net,winforms,C#,.net,Winforms,我有一个TableLayoutPanel,其中包含多行标签。此控件位于具有渐变背景的窗体上。标签通常作为一个整体定期更新。当它们被更新时,它们的更新速度非常慢,并且对用户非常可见 我曾尝试在表单上使用DoubleBuffered,但正如预期的那样,这没有帮助。然后我尝试扩展TableLayoutPanel并设置其DoubleBuffered属性,但这似乎让事情变得更糟。我还尝试在表上设置SuspendLayout(),但这也没有任何效果 我之所以选择此设置,是因为DataGridView不支持透

我有一个
TableLayoutPanel
,其中包含多行标签。此控件位于具有渐变背景的窗体上。标签通常作为一个整体定期更新。当它们被更新时,它们的更新速度非常慢,并且对用户非常可见

我曾尝试在表单上使用
DoubleBuffered
,但正如预期的那样,这没有帮助。然后我尝试扩展
TableLayoutPanel
并设置其
DoubleBuffered
属性,但这似乎让事情变得更糟。我还尝试在表上设置
SuspendLayout()
,但这也没有任何效果

我之所以选择此设置,是因为
DataGridView
不支持透明度。。。然而,我真的很想找到一个替代此设置的方法

似乎相关,但我还没有找到解决问题的方法

有没有关于如何解决更新问题的想法,或者可能的替代方案

编辑因为我要处理固定数量的数据来表示此表,所以我要直接在绘制处理程序中绘制字符串,而不是使用标签。

标签是否停靠(意思是
label.Dock=DockStyle.Fill

每个
标签之间有多少代码。文本
更改

我的应用程序具有完全相同的UI。TableLayoutPanel内的标签TableLayoutPanel内的标签TableLayoutPanel内的标签。但没有梯度背景。每个面板和标签都没有边距和填充物。每个标签都已填充。首先,我们获得每个标签的所有值。只有在这之后,我们才改变所有的标签,但在单独的线程

string[] texts = GetAllLabels();
ownerControl.Invoke(new Action(() =>
{
  for (int i = 0; i < UpdatingControls.Count && i < texts.Count; i++)
  {
    UpdatingControls[i].Text = texts[i];
  }
}));
string[]text=GetAllLabels();
ownerControl.Invoke(新操作(()=>
{
对于(int i=0;i
这将(在我们的例子中)闪烁减少到最小。

标签是否停靠(意思是
label.Dock=DockStyle.Fill

每个
标签之间有多少代码。文本
更改

我的应用程序具有完全相同的UI。TableLayoutPanel内的标签TableLayoutPanel内的标签TableLayoutPanel内的标签。但没有梯度背景。每个面板和标签都没有边距和填充物。每个标签都已填充。首先,我们获得每个标签的所有值。只有在这之后,我们才改变所有的标签,但在单独的线程

string[] texts = GetAllLabels();
ownerControl.Invoke(new Action(() =>
{
  for (int i = 0; i < UpdatingControls.Count && i < texts.Count; i++)
  {
    UpdatingControls[i].Text = texts[i];
  }
}));
string[]text=GetAllLabels();
ownerControl.Invoke(新操作(()=>
{
对于(int i=0;i

这将(在我们的例子中)闪烁减少到最小。

消除闪烁的唯一方法是使用自定义控件(如面板)的绘制处理程序。我尝试的其他方法似乎可以减少闪烁,但没有任何效果

根据我所看到的,标签本身都在做类似的事情,当它们的数据发生变化时,它们会使自己失效。没有办法阻止这个过程,因此最终的效果是将每个标签滚动/延迟更新到下一个标签。这会在复杂背景(如图像)上放大

通过使用绘制处理程序并计算每个“行”的边界,我能够为我的应用程序获得相同的表外观,并且其背景保持透明

这不是一个新颖的代码,但在这里是为了防止它帮助其他人:

private const int RowCount = 10;
private DataTable _table;  // updated elsewhere

private void OnPaint(Object sender, PaintEventArgs e) {
    if (! this.DesignMode) {
        DrawTable(e.Graphics);
    }
}

private void DrawTable(Graphics gfx) {
    SolidBrush brush = new SolidBrush(this.ForeColor);
    for (int rownum = 0; rownum < RowCount; rownum++) {
        RectangleF rect = GetRowBounds(rownum);
        DrawRowNumber(gfx, rect, rownum);

        if (_table.Rows.Count > rownum) {
            DataRow data = _table.Rows[rownum];
            DrawName(gfx, rect, Convert.ToString(data["name"]));
            DrawCount(gfx, rect, Convert.ToSingle(data["count"]));
        }
    }
}

private void DrawRowNumber(Graphics gfx, RectangleF bounds, int place) {
    String str = String.Format("{0}.", (place + 1));
    SolidBrush brush = new SolidBrush(this.ForeColor);
    gfx.DrawString(str, this.Font, brush, bounds.Location);
}

private void DrawName(Graphics gfx, RectangleF bounds, String name) {
    PointF loc = new PointF(80, bounds.Top);
    SolidBrush brush = new SolidBrush(this.ForeColor);
    gfx.DrawString(name, this.Font, brush, loc);
}

private void DrawCount(Graphics gfx, RectangleF bounds, float score) {
    SolidBrush brush = new SolidBrush(this.ForeColor);
    String str = score.ToString("N1");
    SizeF size = gfx.MeasureString(str, this.Font);
    float offset = bounds.Right - size.Width;
    PointF loc = new PointF(offset, bounds.Top);
    gfx.DrawString(str, this.Font, brush, loc);
}

private RectangleF GetRowBounds(int row) {
    if ((row < 0) || (row >= RowCount)) {
        return RectangleF.Empty;
    }

    Rectangle bounds = this.ClientRectangle;
    float height = (float) bounds.Height / (float) RowCount;

    PointF loc = new PointF(bounds.Left, height * row);
    SizeF size = new SizeF(bounds.Width, height);

    return new RectangleF(loc, size);
}
private const int RowCount=10;
专用数据表_table;//其他地方更新
私有void OnPaint(对象发送方,PaintEventArgs e){
如果(!this.DesignMode){
绘图台(如图形);
}
}
专用void绘图台(图形gfx){
SolidBrush笔刷=新的SolidBrush(this.ForeColor);
对于(int-rownum=0;rownumrownum){
数据行数据=_table.Rows[rownum];
DrawName(gfx、rect、Convert.ToString(数据[“名称]));
支取计数(gfx、rect、Convert.ToSingle(数据[“计数]));
}
}
}
私有void DrawRowNumber(图形gfx、矩形边界、整数位置){
String str=String.Format(“{0}.”,(place+1));
SolidBrush笔刷=新的SolidBrush(this.ForeColor);
DrawString(str,this.Font,brush,bounds.Location);
}
私有void DrawName(图形gfx、矩形边界、字符串名称){
PointF loc=新的PointF(80,bounds.Top);
SolidBrush笔刷=新的SolidBrush(this.ForeColor);
gfx.DrawString(名称、字体、画笔、位置);
}
私有无效提取计数(图形gfx、矩形边界、浮动分数){
SolidBrush笔刷=新的SolidBrush(this.ForeColor);
字符串str=score.ToString(“N1”);
SizeF size=gfx.MeasureString(str,this.Font);
float offset=bounds.Right-size.Width;
PointF loc=新的PointF(偏移量、边界.顶部);
gfx.抽绳(str,this.Font,brush,loc);
}
私有矩形GetRowBounds(int行){
如果((行<0)| |(行>=行计数)){
返回矩形F。空;
}
矩形边界=this.ClientRectangle;
浮动高度=(浮动)界限。高度/(浮动)行计数;
PointF loc=新的PointF(bounds.Left,高度*行);
SizeF size=新的SizeF(bounds.Width,height);
返回新矩形F(位置、大小);
}

消除闪烁的唯一方法是使用自定义控件(如面板)的绘制处理程序。我尝试的其他方法似乎可以减少闪烁,但没有任何效果

根据我所看到的,标签本身都在做类似的事情,当它们的数据发生变化时,它们会使自己失效。没有办法阻止这个过程,因此最终的效果是将每个标签滚动/延迟更新到下一个标签。这会在复杂背景(如图像)上放大

通过使用绘制处理程序并计算每个“ro”的边界