1.问题描述
窗体打开时动态绘制控件,后面连接了Socket实时的去刷新panel,重新绘制该区域,发现程序报错,网上查了资料后发现是因为之前创建的控件是由创建UI的线程控制的,后面通信的线程去修改绘制时就会报这个错误。需要委托之前的线程去重新绘制。解决的代码如下:
/// <summary>
/// 绘制滚动文本内容.
/// </summary>
/// <param name="panelTitle">文字标题的容器.</param>
/// <param name="panelContent">文字区域的容器.</param>
private void DrawLabelContent()
{
try
{
var panelTitles = this.splitContainerMain.Panel2.Controls.Find("panelTitle", true);
if (panelTitles != null && panelTitles.Length > 0)
{
// 再获取panelContent
var panelContents = this.splitContainerMain.Panel2.Controls.Find("panelContent", true);
if (panelContents != null && panelContents.Length > 0)
{
var panelTitle = (Panel)panelTitles[0];
var panelContent = (Panel)panelContents[0];
panelContent.Controls.Clear(); // 先清空里面的控件
if (this.exceptionalEquipmentList != null && this.exceptionalEquipmentList.Count > 0)
{
for (int i = 0; i < this.exceptionalEquipmentList.Count; i++)
{
decimal panelRowHeightValue = panelTitle.Height;
int panelRowWidth = panelTitle.Width;
int panelRowHeight = Convert.ToInt32(panelRowHeightValue);
int thisY = panelRowHeight * i;
LabelLastY = thisY;
// 里面再加一个固定的panel
Panel panelRow = new Panel();
panelRow.Name = "panelRow" + i;
panelRow.Height = Convert.ToInt32(panelRowHeight);
panelRow.Width = panelRowWidth;
panelRow.BorderStyle = BorderStyle.None;
panelRow.BackColor = Color.Transparent; // 透明
panelRow.Location = new Point(0, thisY); // y就是第几行的y值
panelContent.Controls.Add(panelRow); // contentControl.Controls.Add(panelRow);
// 添加标题
decimal labeRow1With = panelTitle.Width * FormConst.Row1ItemPanelWidthPont;
decimal labeRow2With = panelTitle.Width * FormConst.Row2ItemPanelWidthPont;
decimal labeRow3With = panelTitle.Width * FormConst.Row3ItemPanelWidthPont;
decimal labeRow4With = panelTitle.Width * FormConst.Row4ItemPanelWidthPont;
decimal labeRow5With = panelTitle.Width * FormConst.Row5ItemPanelWidthPont;
// 工序
Label Label_ProcessesR = new Label();
Label_ProcessesR.Height = panelTitle.Height;
Label_ProcessesR.Width = Convert.ToInt32(labeRow1With);
Label_ProcessesR.Location = new Point(0, 0);
Label_ProcessesR.Text = exceptionalEquipmentList[i].procType.ToString();
Label_ProcessesR.TextAlign = ContentAlignment.MiddleCenter;
// 偏离控制 黄色
if (exceptionalEquipmentList[i].outRangeType == "1" || exceptionalEquipmentList[i].outRangeType == "3")
{
Label_ProcessesR.ForeColor = FormConst.LabelColorYellow;
}
else
{
Label_ProcessesR.ForeColor = Color.Red;
}
panelRow.Controls.Add(Label_ProcessesR);
// 设备名称
Label Label_EquipmnetNameR = new Label();
Label_EquipmnetNameR.Height = panelRowHeight;
Label_EquipmnetNameR.Width = Convert.ToInt32(labeRow2With);
Label_EquipmnetNameR.Location = new Point(Label_ProcessesR.Width, 0);
Label_EquipmnetNameR.Text = exceptionalEquipmentList[i].displayEqpIdShow;
Label_EquipmnetNameR.TextAlign = ContentAlignment.MiddleCenter;
// 偏离控制 黄色
if (exceptionalEquipmentList[i].outRangeType == "1" || exceptionalEquipmentList[i].outRangeType == "3")
{
Label_EquipmnetNameR.ForeColor = FormConst.LabelColorYellow;
}
else
{
Label_EquipmnetNameR.ForeColor = Color.Red;
}
panelRow.Controls.Add(Label_EquipmnetNameR);
// 测试项目
Label Label_TestProjectR = new Label();
Label_TestProjectR.Height = panelRowHeight;
Label_TestProjectR.Width = Convert.ToInt32(labeRow3With);
Label_TestProjectR.Location = new Point(Label_EquipmnetNameR.Location.X + Label_EquipmnetNameR.Width, 0);
Label_TestProjectR.Text = exceptionalEquipmentList[i].disPlayParamDesc;
Label_TestProjectR.TextAlign = ContentAlignment.MiddleCenter;
// 偏离控制 黄色
if (exceptionalEquipmentList[i].outRangeType == "1" || exceptionalEquipmentList[i].outRangeType == "3")
{
Label_TestProjectR.ForeColor = FormConst.LabelColorYellow;
}
else
{
Label_TestProjectR.ForeColor = Color.Red;
}
panelRow.Controls.Add(Label_TestProjectR);
// 测试结果
Label Label_TestResultR = new Label();
Label_TestResultR.Height = panelRowHeight;
Label_TestResultR.Width = Convert.ToInt32(labeRow4With);
Label_TestResultR.Location = new Point(Label_TestProjectR.Location.X + Label_TestProjectR.Width, 0);
Label_TestResultR.Text = exceptionalEquipmentList[i].paramValue;
Label_TestResultR.TextAlign = ContentAlignment.MiddleCenter;
// 偏离控制 黄色
if (exceptionalEquipmentList[i].outRangeType == "1" || exceptionalEquipmentList[i].outRangeType == "3")
{
Label_TestResultR.ForeColor = FormConst.LabelColorYellow;
}
else
{
Label_TestResultR.ForeColor = Color.Red;
}
panelRow.Controls.Add(Label_TestResultR);
// 状态
Label Label_StateR = new Label();
Label_StateR.Height = panelRowHeight;
Label_StateR.Width = Convert.ToInt32(labeRow5With);
Label_StateR.Location = new Point(Label_TestResultR.Location.X + Label_TestResultR.Width, 0);
Label_StateR.Text = exceptionalEquipmentList[i].outRangeDesc;
Label_StateR.TextAlign = ContentAlignment.MiddleCenter;
// 偏离控制 黄色
if (exceptionalEquipmentList[i].outRangeType == "1" || exceptionalEquipmentList[i].outRangeType == "3")
{
Label_StateR.ForeColor = FormConst.LabelColorYellow;
}
else
{
Label_StateR.ForeColor = Color.Red;
}
panelRow.Controls.Add(Label_StateR);
}
}
}
}
}
catch (Exception ex)
{
logHelper.SetErrorLog("DrawLabelContent 异常:" + ex.Message);
}
}
委托的代码如下:
// 这边需要用委托因为跨线程了
this.Invoke(new MethodInvoker(delegate { DrawLabelContent(); }));