private void SetBtnColor(uButton btn, Color color) { if (color == Color.Yellow) { btn.LanternBackground = Color.Yellow; } else if (color == Color.Green) { btn.LanternBackground = Color.Green; } else if (color == Color.Blue) { btn.LanternBackground = Color.Blue; } else if (color == Color.Red) { btn.LanternBackground = Color.Red; } else if (color == Color.Gray) { btn.LanternBackground = Color.Gray; } else { btn.LanternBackground = color; } }
private void updateUI(object obj) { foreach (KeyValuePair <int, Index> pair in t.dic_Index) { int col = pair.Value.Col; int row = pair.Value.Row; uButton btn = (uButton)tableLayoutPanel1.GetControlFromPosition(col, row); SetBtnColor(btn, pair.Value.color); } }
public void CreateUnRegular2() { if (bShowModel) { return; } for (int i = 0; i < Row; i++) { for (int j = 0; j < Col; j++) { if (i % 2 == 0) { if (j % 2 == 1) { uButton btn = (uButton)tableLayoutPanel1.GetControlFromPosition(j, i); SetBtnColor(btn, backColor); t.AddEmptyPos(btn.Name); } else { uButton btn = (uButton)tableLayoutPanel1.GetControlFromPosition(j, i); SetBtnColor(btn, Color.Blue); t.RemoveEmptyPos(btn.Name); } } else { if (j % 2 == 0) { uButton btn = (uButton)tableLayoutPanel1.GetControlFromPosition(j, i); SetBtnColor(btn, backColor); t.AddEmptyPos(btn.Name); } else { uButton btn = (uButton)tableLayoutPanel1.GetControlFromPosition(j, i); SetBtnColor(btn, Color.Blue); t.RemoveEmptyPos(btn.Name); } } } } }
/// <summary> /// 动态布局 /// </summary> /// <param name="layoutPanel">布局面板</param> /// <param name="Row">行</param> /// <param name="Col">列</param> private void DynamicLayout(TableLayoutPanel layoutPanel, Color bColor, int size = 32) { layoutPanel.Controls.Clear(); layoutPanel.RowStyles.Clear(); layoutPanel.ColumnStyles.Clear(); layoutPanel.RowCount = Row + 1; //设置分成几行 float pRow = Convert.ToSingle((100 / Row).ToString("0.00")); float pColumn = Convert.ToSingle((100 / Col).ToString("0.00")); for (int i = 0; i < Row; i++) { layoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, size)); } layoutPanel.ColumnCount = Col + 1; //设置分成几列 for (int i = 0; i < Col; i++) { layoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, size)); } for (int i = 0; i < Row; i++) { for (int j = 0; j < Col; j++) { uButton btn = new uButton(); btn.Anchor = AnchorStyles.None; btn.Margin = new Padding(0, 0, 0, 0); btn.Dock = DockStyle.Fill; btn.BackColor = Color.Black; btn.Font = new Font("宋体", 7.5F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(134))); btn.Name = i.ToString() + "_" + j.ToString(); Index pos = new Index(i, j); int num = t.FindPos(pos); if (num > -1) { t.dic_Index[num].SetColor(bColor);//初始化颜色 } //如果是显示模式 if (bShowModel) { //屏蔽的点不显示名称 if (num < 0) { btn.Text = ""; SetBtnColor(btn, backColor); } else { //不屏蔽的点显示顺序号 btn.Text = num.ToString(); SetBtnColor(btn, bColor); } } else { if (num < 0) { SetBtnColor(btn, backColor); } else { SetBtnColor(btn, bColor); } btn.Text = i.ToString() + "," + j.ToString(); } btn.Click += new EventHandler(this.label_Click); layoutPanel.Controls.Add(btn, j, i); } } }