如何删除控件
C# visual studio 2010 程序为监控电池电压,在tabpage 里 采用 chart 方式 显示每个电芯电压,当一tabpage显示不了所有电池信息时,会自动添加tabpage以便显示所有电池信息。
要求是当外接信号源断开时,删除所有的chart,以及添加的tabpage。因为再次接上的电池可能第一个tabpage就能全部显示。
注:第一页(页名:tabPageCellDetail1)总是保留,只删除添加的页面。
删除部分的代码如下:
private void clearChart(int batNum, byte batCell) { if (batCell> 5) { pageNo = (byte)(batNum / 10 + 1); } else { pageNo = (byte)(batNum / 19 + 1); } if (pageNo > 2) { for (int ii = 2; ii <= pageNo; ii++) { string name = "tabPageCellDetail" + ii.ToString(); tabPageCellDetail1.Controls.RemoveByKey(name); } } else { foreach (Control charts in tabPageCellDetail1.Controls) { tabPageCellDetail1.Controls.Remove(charts); } } }
private void clearChart(int batNum, byte batCell) { if (batCell> 5) { pageNo = (byte)(batNum / 10 + 1); } else { pageNo = (byte)(batNum / 19 + 1); } if (pageNo > 2) { foreach (Control findControl in this.Controls) { TabPage findTabPage = (findControl as TabPage); if (findControl.Name.Length > 17) { //first page, remain page, remove control if (findControl.Name == "tabPageCellDetail1") { foreach (Control charts in tabPageCellDetail1.Controls) { tabPageCellDetail1.Controls.Remove(charts); } } else { string tempStr = string.Empty; tempStr = findControl.Name.Substring(0, 17); if (tempStr == "tabPageCellDetail") { //not first page, remove page and control foreach (Control charts in findControl.Controls) { findControl.Controls.Remove(charts); } this.Controls.Remove(findControl); } } } } } else { foreach (Control charts in tabPageCellDetail1.Controls) { tabPageCellDetail1.Controls.Remove(charts); } } }