为什么这个FlowLayoutPanel列表是以一种奇怪的模式绘制的?

时间:2014-08-23 11:12:22

标签: c# asp.net winforms c#-4.0 flowlayoutpanel

我有一个程序可以将较小的FlowLayoutPanels添加到较大的Panel,或者至少是意图。每个FlowLayoutPanel都包含其他几个简单的Controls,并存储在名为skillPanels的List<FlowLayoutPanel>中。

单击该按钮将新FlowLayoutPanel添加到Panel时,会生成一个并存储在skillPanel中。然后将skillPanels深度复制到持有者(名为,err ... holder),然后彻底删除以消除UI中可见的FlowLayoutPanels - 每个FlowLayoutPanel都有其子项Dispose-d,然后是Disposed本身。为了更好地衡量,List就是.Clear() - ed。

清理平板后,将支架深度复制回skillPanels,然后对其进行迭代并将其成员添加到较大的Panel,每个人手动定位,因为我不相信布局管理器,我可以丢掉它们。对于将来添加的for-each-FlowLayoutPanel删除方法,可悲地需要这种迂回程度。通过单步执行代码并提供MessageBoxes,我确信每个代码都会添加到较大的Panel

但这里是踢球者:尽管.Add - Panel,但只有部分出现。.Dispose我怀疑FlowLayoutPanel方法应该归咎于某种方式,但我无法解释如何或为什么。

为了我自己的理智,以及一定程度的清晰度,这是using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { List<FlowLayoutPanel> skillPanels = new List<FlowLayoutPanel>(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Panel pnlMacroSkillsPanel = new Panel(); pnlMacroSkillsPanel.Height = this.Height; pnlMacroSkillsPanel.Width = this.Width; Button btnAddSkill = new Button(); btnAddSkill.Click += new EventHandler(btnAddSkill_Click); pnlMacroSkillsPanel.Controls.Add(btnAddSkill); this.Controls.Add(pnlMacroSkillsPanel); } void reDrawSkillPanels(Panel pan) { FlowLayoutPanel plato = new FlowLayoutPanel(); List<FlowLayoutPanel> holder = new List<FlowLayoutPanel>(skillPanels.Count); foreach (FlowLayoutPanel f in skillPanels) { holder.Add(f); } try { foreach (object f in pan.Controls) { if (f is FlowLayoutPanel) { plato = (FlowLayoutPanel)f; MessageBox.Show("Disposing of FlowLayoutPanel"); foreach (Control c in plato.Controls) { c.Dispose(); } ((FlowLayoutPanel)f).Dispose(); } } skillPanels.Clear(); } catch (Exception e) { // } foreach (FlowLayoutPanel f in holder) { skillPanels.Add(f); } holder.Clear(); int initialYoffset = 40; for (int count = 0; count < skillPanels.Count; count++) { plato = new FlowLayoutPanel(); plato = skillPanels[count]; plato.Top = initialYoffset + ((plato.Height + 4) * count); plato.Left = 4; //MessageBox.Show("Adding skillPanels member at: 4, " + (initialYoffset + ((skillPanels[count].Height + 4) * count)) + "!"); //MessageBox.Show("ToString:" + skillPanels[count].ToString() + ", Width: " + skillPanels[count].Width); pan.Controls.Add(plato); } //MessageBox.Show("skillPanels.Count = " + skillPanels.Count); } void btnAddSkill_Click(object sender, EventArgs e) { Button plato = (Button)sender; FlowLayoutPanel pnlNewSkill = new FlowLayoutPanel pnlNewSkill.FlowDirection = FlowDirection.LeftToRight; pnlNewSkill.Height = 30; pnlNewSkill.Width = 350; Random r = new Random(); Color randomColor = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255)); pnlNewSkill.BackColor = randomColor; /* * Code creating and adding Controls to pnlNewPanel would go here */ skillPanels.Add(pnlNewSkill); reDrawSkillPanels(((Panel)(plato.Parent))); } } } 在点击之后可见的模式:

第一次加:1

第二次加:2次(即1已经消失)

第3次添加:2,3

第4次添加:2,4

第5次补充:2,4,5

6th add:4,6

7th add:4,6,7

第8次添加:6,8

9th add:6,8,9

看到形成的图案?

{{1}}

1 个答案:

答案 0 :(得分:1)

当您将列表添加到列表skillPanels时,即使您稍后使用pan.Controls.Add添加该面板,它也会保留在那里。因此,当第二次单击时,您Dispose,然后再次添加它,以及新面板。第三次点击后,您Dispose第二个,现在您的列表包含2个已配置的面板和一个新的面板。您添加到pan等所有这些内容

所以基本上,你在skillPanels列表中堆积已处理的面板,然后在pan.Controls循环中继续将它们读到for,尽管事实上它们是&# 39;已被处置。

不确定您需要列表 - 您可以直接在btnAddSkill_Click添加新面板。另一种可能性是将它们添加到pan.Controls或处理它们后从列表中删除它们。