gpt4 book ai didi

c# - TableLayoutPanel 更新滞后

转载 作者:行者123 更新时间:2023-12-04 10:10:45 25 4
gpt4 key购买 nike

我有一个 WinForms 应用程序,它正在从 BackgroundWorker 更新 TableLayoutPanel。根据设备输入是打开 (1) 还是关闭 (0),颜色和一些文本会相应地更改。

try
{
//Define a new TLP to hold the search tablelayoutpanel.
//search for the iterated card number.
//get the status label using GetControl from Position.
TableLayoutPanel TLP = new TableLayoutPanel();
string IO_Card_Name = "ED527_" + i.ToString();
TLP = TLP_IO_Info.Controls.Find(IO_Card_Name, true).FirstOrDefault() as TableLayoutPanel;
try { lbl = TLP.GetControlFromPosition(4, 0) as Label; } catch { lbl = null; };
//if card is found (because input is active, colour the TLP (card) according to its state.
if (TLP != null && lbl != null && INPUTS[i - 1] == 1)
{

TLP.BackColor = Color.Green;

foreach (Label l in TLP.Controls)
{
l.BackColor = Color.Green;
}
lbl.Invoke((MethodInvoker)delegate { lbl.Text = "ON"; });
}
else if (TLP != null && lbl != null && INPUTS[i - 1] == 0)
{
TLP.BackColor = Color.White;
foreach (Label l in TLP.Controls)
{
l.BackColor = Color.White;
}
lbl.Invoke((MethodInvoker)delegate { lbl.Text = "OFF"; });
}
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
};
TLP里面有 5 个标签。当线路更新时,更新显示一些明显的延迟。有什么方法可以执行类似于 SuspendLayout() 的操作吗?/ ResumeLayout在主 UI 线程上?

****编辑以显示之前和之后 - IOLabel 列在 Status 列之前稍微更新。

Table Layout Panel

最佳答案

听起来你有一个嵌套的设计。每行有 5 个标签由不同的 TableLayoutPanels 和 TLP_IO_Info 托管。这是一个 TableLayoutPanel 承载其他 TableLayoutPanel。在 DoWork BackgroundWorker 的事件你有一个 for..loop更改 Backcolor根据您从 INPUT 读取的设备的当前状态设置内部控件整数数组。请纠正我。

我想这样建议:

foreach (var tlp in TLP_IO_Info.Controls.OfType<TableLayoutPanel>()
.Where(x => x.Name.StartsWith("ED527_")))
{
if (tlp.GetControlFromPosition(4, 0) is Label lbl)
{
var state = // get the state of the current device from INPUT array
var stateColor = state == 1 ? Color.Green : Color.White;
var stateText = state == 1 ? "ON" : "OFF";
this.Invoke(new Action(() =>
{
tlp.BackColor = stateColor;
tlp.Controls.OfType<Label>().ToList().ForEach(l => l.BackColor = stateColor);
lbl.Text = stateText;
}));
}
}

或者这样可以消除冗余代码:

var stateColors = new[] { Color.White, Color.Green };
var stateTexts = new[] { "OFF", "ON" };

foreach (var tlp in TLP_IO_Info.Controls.OfType<TableLayoutPanel>()
.Where(x => x.Name.StartsWith("ED527_")))
{
if (tlp.GetControlFromPosition(4, 0) is Label lbl)
{
var state = // get the state of the current device from INPUT array
this.Invoke(new Action(() =>
{
tlp.BackColor = stateColors[state];
tlp.Controls.OfType<Label>().ToList()
.ForEach(l => l.BackColor = stateColors[state]);
lbl.Text = stateTexts[state];
}));
}
}

请注意,我已经删除了昂贵的 try..catch块,因为此代码不会抛出任何异常。

至于 INPUT数组,我建议你用 Dictionary<string, int> 替换它存储每个设备的当前状态,因为(根据您提供的链接)每个设备都有一个唯一的 IOLineNumber因此您可以轻松设置/获取每个的当前状态。

⍰ 可能图书馆里已经有这样的东西了吧?

关于c# - TableLayoutPanel 更新滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61351569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com