怎样随机在 panel容器中 排布 button 控件
就好像,上图一样,三个容器里面是有 三个按钮控件的,当我点击一下 按钮4 三个容器里面的按钮控件会出现
问题:怎么可以 让那三个按钮 随机 排布在不同的容器里面 (比如 按钮1 可以在 容器 1,2,3 都可以出现)
如果我不止三个容器和三个按钮,也怎么解决····
private void ShowButton(Panel[] panels) { if (panels == null) return; Random random = new Random(); int count = panels.Length; Button temp; for (int i = 0; i < count; i++) { int val = random.Next(count); if (val != i) { temp = panels[i].Controls[0] as Button; panels[i].Controls.Clear(); panels[i].Controls.Add(panels[val].Controls[0] as Button); panels[val].Controls.Clear(); panels[val].Controls.Add(temp); } } }
private void button4_Click(object sender, EventArgs e) { Panel[] panels=new Panel[] { panel1, panel2, panel3 };//填充panels数组,你有32个全部加进去 foreach (Panel p in panels) ((Button)p.Controls[0]).Visible = true; ShowButton(panels); }
foreach (Panel p in panels) { if (p.HasChildren) ((Button)p.Controls[0]).Visible = true; }那就多加一个判断