private void LoadPanels() { try { if (setting2.Guards.Count < 1) { panel1.Visible = false; ClearPanel(); } else { panel1.BackColor = disconnectColor; Dictionary <int, string> locDict = new Dictionary <int, string>(); for (int i = 0; i < setting2.Guards.Count; i++) { Guard guard = setting2.Guards[i]; List <Control> cs = new List <Control>(); if (i == 0) { panel1.Visible = true; locDict.Add(0, panel1.Location.X + "," + panel1.Location.Y); } else { int count = pnlControls.Controls.Cast <Control>().Count(c => c.GetType() == typeof(Panel)); if (count <= i) { Panel pnl = ControlExtensions.Clone(panel1); pnl.Name = panel1.Name + i; pnl.MouseLeave += new System.EventHandler(this.panel_MouseLeave); pnl.MouseHover += new System.EventHandler(this.panel_MouseHover); //设置横向间距以及是否换行 int x = panel1.Width * i + panel1.Location.X * (i + 1); int x1 = panel1.Width * (i + 1) + panel1.Location.X * (i + 2); int y = panel1.Location.Y; if (x1 > pnlControls.Width) { if (maxCount == 0) { maxCount = i; } int mod = i % maxCount; string[] tmp = locDict[mod].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); x = Convert.ToInt32(tmp[0]); int rowIndex = (int)Math.Ceiling((double)(i + 1) / maxCount); y = panel1.Height * (rowIndex - 1) + panel1.Location.Y * (rowIndex); } pnl.Location = new Point(x, y); locDict.Add(i, x + "," + y); pnl.BackColor = disconnectColor; pnlControls.Controls.Add(pnl); foreach (Label l in panel1.Controls.Cast <Control>().OrderBy(a => Convert.ToInt32(a.Tag))) { Label newlabel = ControlExtensions.Clone(l); newlabel.Name = l.Name + i; pnl.Controls.Add(newlabel); cs.Add(newlabel); } } } cs = pnlControls.Controls[i].Controls.Cast <Control>().OrderBy(a => Convert.ToInt32(a.Tag)).ToList(); AssignControl(cs, guard); } ClearPanel(); } } catch (Exception ex) { } }