// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Drawing; using System.Windows.Forms; public class MainForm : Form { private const int N = 4; private readonly Color[] cols = { Color.Red, Color.Green, Color.Blue }; private Button[] btn; private Panel[] pan; private void BtnClick(object sender, EventArgs e) { int sel = Array.IndexOf(btn, sender); for(int i = 0; i < N; i++) { if(i == sel) { pan[i].Visible = true; } else { pan[i].Visible = false; } } } private void InitializeComponent() { SuspendLayout(); btn = new Button[N]; pan = new Panel[N]; for(int i = 0; i < N; i++) { btn[i] = new Button(); btn[i].Text = "Show panel #" + (i+1); btn[i].Size = new Size(75, 50); btn[i].Location = new Point(25+i*100, 200); btn[i].Click += new EventHandler(BtnClick); Controls.Add(btn[i]); pan[i] = new Panel(); Label lbl = new Label(); lbl.Text = "Panel #" + (i+1); lbl.Font = new Font(FontFamily.GenericSerif, 24.0f); lbl.Size = new Size(N*100-25, 150); lbl.Location = new Point(25, 25); pan[i].Controls.Add(lbl); pan[i].BackColor = cols[i%cols.Length]; pan[i].Size = new Size(N*100-25, 150); pan[i].Location = new Point(25, 25); pan[i].Visible = (i == 0); Controls.Add(pan[i]); } ClientSize = new Size(25+N*100, 275); Text = "Panel fun"; ResumeLayout(false); } public MainForm() { InitializeComponent(); } [STAThread] public static void Main(string[] args) { Application.Run(new MainForm()); } }