asked on
ASKER
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
namespace EE_Q29016206
{
public partial class Form1 : Form
{
readonly List<Form> ChildForms = new List<Form>();
public Form1()
{
InitializeComponent();
}
private void OnClick(object sender, EventArgs e)
{
if (sender is Button)
{
var btn = sender as Button;
var action = btn.Text.Split(' ');
if (action.Length == 2)
{
var form = ChildForms.Where(x => x.Name.Equals(action[1])).FirstOrDefault();
if (form == null)
{
switch (action[1])
{
case "Form2":
form = new Form2();
break;
case "Form3":
form = new Form3();
break;
case "Form4":
form = new Form4();
break;
case "Form5":
form = new Form5();
break;
}
ChildForms.Add(form);
}
switch (action[0])
{
case "Show":
form.Show();
btn.Text = string.Format("Hide {0}", form.Name);
break;
case "Hide":
form.Hide();
btn.Text = string.Format("Show {0}", form.Name);
break;
}
}
}
}
}
}
Form1.Designer.cs -
namespace EE_Q29016206
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(13, 13);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(259, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Show Form2";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.OnClick);
//
// button2
//
this.button2.Location = new System.Drawing.Point(13, 42);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(259, 23);
this.button2.TabIndex = 1;
this.button2.Text = "Show Form3";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.OnClick);
//
// button3
//
this.button3.Location = new System.Drawing.Point(13, 71);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(259, 23);
this.button3.TabIndex = 2;
this.button3.Text = "Show Form4";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.OnClick);
//
// button4
//
this.button4.Location = new System.Drawing.Point(13, 100);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(259, 23);
this.button4.TabIndex = 3;
this.button4.Text = "Show Form5";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.OnClick);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 129);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
}
}
Form2.cs -
using System;
using System.Windows.Forms;
namespace EE_Q29016206
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void OnLoad(object sender, EventArgs e)
{
label1.Text = string.Format(label1.Text, Name);
}
}
}
Form2.Designer.cs -
namespace EE_Q29016206
{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(133, 20);
this.label1.TabIndex = 1;
this.label1.Text = "This is form {0}.";
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 106);
this.Controls.Add(this.label1);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.OnLoad);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
}
}
(rinse and repeat Fomr2 for Form3, Form4 and Form5)...ASKER
ASKER
ASKER
ASKER
This topic area includes legacy versions of Windows prior to Windows 2000: Windows 3/3.1, Windows 95 and Windows 98, plus any other Windows-related versions including Windows Mobile.
TRUSTED BY