/* User Interfaces in C#: Windows Forms and Custom Controls by Matthew MacDonald Publisher: Apress ISBN: 1590590457 */ using System.Data.SqlClient; using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace DynamicMenu { /// <summary> /// Summary description for DynamicMenu. /// </summary> public class DynamicMenu : System.Windows.Forms.Form { internal System.Windows.Forms.MainMenu mnuMain; internal System.Windows.Forms.MenuItem mnuFile; internal System.Windows.Forms.MenuItem mnuNew; internal System.Windows.Forms.MenuItem mnuOpen; internal System.Windows.Forms.MenuItem mnuClose; internal System.Windows.Forms.MenuItem mnuSave; internal System.Windows.Forms.MenuItem MenuItem12; internal System.Windows.Forms.MenuItem mnuExit; internal System.Windows.Forms.MenuItem mnuTools; internal System.Windows.Forms.MenuItem mnuManageHardware; internal System.Windows.Forms.MenuItem mnuSetupUserAccounts; internal System.Windows.Forms.MenuItem mnuChangeDisplay; internal System.Windows.Forms.MenuItem mnuHelp; internal System.Windows.Forms.MenuItem mnuContents; internal System.Windows.Forms.MenuItem MenuItem13; internal System.Windows.Forms.MenuItem mnuAbout; internal System.Windows.Forms.Button cmdAdmin; internal System.Windows.Forms.Button cmdUser; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public DynamicMenu() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (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.mnuMain = new System.Windows.Forms.MainMenu(); this.mnuFile = new System.Windows.Forms.MenuItem(); this.mnuNew = new System.Windows.Forms.MenuItem(); this.mnuOpen = new System.Windows.Forms.MenuItem(); this.mnuClose = new System.Windows.Forms.MenuItem(); this.mnuSave = new System.Windows.Forms.MenuItem(); this.MenuItem12 = new System.Windows.Forms.MenuItem(); this.mnuExit = new System.Windows.Forms.MenuItem(); this.mnuTools = new System.Windows.Forms.MenuItem(); this.mnuManageHardware = new System.Windows.Forms.MenuItem(); this.mnuSetupUserAccounts = new System.Windows.Forms.MenuItem(); this.mnuChangeDisplay = new System.Windows.Forms.MenuItem(); this.mnuHelp = new System.Windows.Forms.MenuItem(); this.mnuContents = new System.Windows.Forms.MenuItem(); this.MenuItem13 = new System.Windows.Forms.MenuItem(); this.mnuAbout = new System.Windows.Forms.MenuItem(); this.cmdAdmin = new System.Windows.Forms.Button(); this.cmdUser = new System.Windows.Forms.Button(); this.SuspendLayout(); // // mnuMain // this.mnuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuFile, this.mnuTools, this.mnuHelp}); // // mnuFile // this.mnuFile.Index = 0; this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuNew, this.mnuOpen, this.mnuClose, this.mnuSave, this.MenuItem12, this.mnuExit}); this.mnuFile.Text = "File"; // // mnuNew // this.mnuNew.Index = 0; this.mnuNew.Text = "New"; // // mnuOpen // this.mnuOpen.Index = 1; this.mnuOpen.Text = "Open"; // // mnuClose // this.mnuClose.Index = 2; this.mnuClose.Text = "Close"; // // mnuSave // this.mnuSave.Index = 3; this.mnuSave.Text = "Save"; // // MenuItem12 // this.MenuItem12.Index = 4; this.MenuItem12.Text = "-"; // // mnuExit // this.mnuExit.Index = 5; this.mnuExit.Text = "Exit"; // // mnuTools // this.mnuTools.Index = 1; this.mnuTools.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuManageHardware, this.mnuSetupUserAccounts, this.mnuChangeDisplay}); this.mnuTools.Text = "Tools"; // // mnuManageHardware // this.mnuManageHardware.Index = 0; this.mnuManageHardware.Text = "Manage Hardware"; // // mnuSetupUserAccounts // this.mnuSetupUserAccounts.Index = 1; this.mnuSetupUserAccounts.Text = "Setup User Accounts"; // // mnuChangeDisplay // this.mnuChangeDisplay.Index = 2; this.mnuChangeDisplay.Text = "Change Display"; // // mnuHelp // this.mnuHelp.Index = 2; this.mnuHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuContents, this.MenuItem13, this.mnuAbout}); this.mnuHelp.Text = "Help"; // // mnuContents // this.mnuContents.Index = 0; this.mnuContents.Text = "Contents"; // // MenuItem13 // this.MenuItem13.Index = 1; this.MenuItem13.Text = "-"; // // mnuAbout // this.mnuAbout.Index = 2; this.mnuAbout.Text = "About"; // // cmdAdmin // this.cmdAdmin.FlatStyle = System.Windows.Forms.FlatStyle.System; this.cmdAdmin.Location = new System.Drawing.Point(132, 88); this.cmdAdmin.Name = "cmdAdmin"; this.cmdAdmin.Size = new System.Drawing.Size(80, 24); this.cmdAdmin.TabIndex = 3; this.cmdAdmin.Text = "Admin Level"; this.cmdAdmin.Click += new System.EventHandler(this.cmdAdmin_Click); // // cmdUser // this.cmdUser.FlatStyle = System.Windows.Forms.FlatStyle.System; this.cmdUser.Location = new System.Drawing.Point(44, 88); this.cmdUser.Name = "cmdUser"; this.cmdUser.Size = new System.Drawing.Size(80, 24); this.cmdUser.TabIndex = 2; this.cmdUser.Text = "User Level"; this.cmdUser.Click += new System.EventHandler(this.cmdUser_Click); // // DynamicMenu // this.AutoScaleBaseSize = new System.Drawing.Size(5, 14); this.ClientSize = new System.Drawing.Size(256, 129); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.cmdAdmin, this.cmdUser}); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.Menu = this.mnuMain; this.Name = "DynamicMenu"; this.Text = "Dynamic Menu"; this.Load += new System.EventHandler(this.DynamicMenu_Load); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new DynamicMenu()); } private void DynamicMenu_Load(object sender, System.EventArgs e) { cmdUser_Click(null,null); } private void cmdUser_Click(object sender, System.EventArgs e) { DataTable dtPermissions; // Get permissions for an Admin-level user. dtPermissions = DBPermissions.GetPermissions(DBPermissions.Level.User); // Update the menu with these permissions. SearchMenu(this.Menu, dtPermissions); } private void cmdAdmin_Click(object sender, System.EventArgs e) { DataTable dtPermissions; // Get permissions for an Admin-level user. dtPermissions = DBPermissions.GetPermissions(DBPermissions.Level.Admin); // Update the menu with these permissions. SearchMenu(this.Menu, dtPermissions); } private void SearchMenu(Menu menu, DataTable dtPermissions) { DataRow[] rowMatch; foreach (MenuItem mnuItem in menu.MenuItems) { // See if this menu item has a corresponding row. rowMatch = dtPermissions.Select("ControlName = '" + mnuItem.Text + "'"); // If it does, configure the menu item state accordingly. if (rowMatch.GetLength(0) > 0) { DBPermissions.State state; state = (DBPermissions.State)int.Parse(rowMatch[0]["State"].ToString()); switch (state) { case DBPermissions.State.Hidden: mnuItem.Visible = false; break; case DBPermissions.State.Disabled: mnuItem.Enabled = false; break; } } else { mnuItem.Visible = true; mnuItem.Enabled = true; } // Search recursively through any submenus. if (mnuItem.MenuItems.Count > 0) { SearchMenu(mnuItem, dtPermissions); } } } } public class DBPermissions { public enum State { Normal = 0, Disabled = 1, Hidden = 2 } public enum Level { Admin, User } private static SqlConnection con = new SqlConnection("Data Source=localhost;" + "Integrated Security=SSPI;Initial Catalog=Apress;"); public static DataTable GetPermissions(Level userLevel) { con.Open(); // Permissions isn't actually actually a table in our data source. // Instead, it's a view that combines the important information // from all three tables using a Join query. string selectPermissions = "SELECT * FROM Permissions "; switch (userLevel) { case Level.Admin: selectPermissions += "WHERE LevelName = 'Admin'"; break; case Level.User: selectPermissions += "WHERE LevelName = 'User'"; break; } SqlCommand cmd = new SqlCommand(selectPermissions, con); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adapter.Fill(ds, "Permissions"); con.Close(); return ds.Tables["Permissions"]; } } } /* -- Database Dump Wizard Ver 2.2.1 (libmssql version 2.3) -- -- Host: localhost Database: [Apress] -- --------------------------------------------------------- -- Server version Microsoft SQL Server Version 8.0.255 -- Dumping user-defined datatypes in '[Apress]' -- Table structure for table 'Controls' CREATE TABLE [Controls] ( [ID] int IDENTITY NOT NULL, [ControlName] varchar(20) NOT NULL) GO CREATE UNIQUE CLUSTERED INDEX PK_Controls ON [Controls] (ID) GO -- Dumping data for table 'Controls' -- -- Enable identity insert SET IDENTITY_INSERT [Controls] ON GO INSERT INTO [Controls] ([ID], [ControlName]) VALUES(1, 'New') GO INSERT INTO [Controls] ([ID], [ControlName]) VALUES(2, 'Save') GO INSERT INTO [Controls] ([ID], [ControlName]) VALUES(3, 'Manage Hardware') GO INSERT INTO [Controls] ([ID], [ControlName]) VALUES(4, 'Setup User Accounts') GO -- Disable identity insert SET IDENTITY_INSERT [Controls] OFF GO -- Table structure for table 'Controls_Levels' CREATE TABLE [Controls_Levels] ( [ID] int IDENTITY NOT NULL, [Control_ID] int NOT NULL, [Level_ID] int NOT NULL, [State] smallint) GO CREATE UNIQUE CLUSTERED INDEX PK_Control_Levels ON [Controls_Levels] (ID) GO -- Dumping data for table 'Controls_Levels' -- -- Enable identity insert SET IDENTITY_INSERT [Controls_Levels] ON GO INSERT INTO [Controls_Levels] ([ID], [Control_ID], [Level_ID], [State]) VALUES(1, 1, 2, 1) GO INSERT INTO [Controls_Levels] ([ID], [Control_ID], [Level_ID], [State]) VALUES(2, 2, 2, 1) GO INSERT INTO [Controls_Levels] ([ID], [Control_ID], [Level_ID], [State]) VALUES(3, 3, 2, 2) GO INSERT INTO [Controls_Levels] ([ID], [Control_ID], [Level_ID], [State]) VALUES(4, 4, 2, 2) GO -- Disable identity insert SET IDENTITY_INSERT [Controls_Levels] OFF GO -- Table structure for table 'Levels' CREATE TABLE [Levels] ( [ID] int IDENTITY NOT NULL, [LevelName] varchar(50)) GO CREATE UNIQUE CLUSTERED INDEX PK_UserLevels ON [Levels] (ID) GO -- Dumping data for table 'Levels' -- -- Enable identity insert SET IDENTITY_INSERT [Levels] ON GO INSERT INTO [Levels] ([ID], [LevelName]) VALUES(1, 'Admin') GO INSERT INTO [Levels] ([ID], [LevelName]) VALUES(2, 'User') GO -- Disable identity insert SET IDENTITY_INSERT [Levels] OFF GO CREATE VIEW [Permissions] AS SELECT Controls.ControlName, Levels.LevelName, Controls_Levels.State FROM Controls INNER JOIN Controls_Levels ON Controls.ID = Controls_Levels.Control_ID INNER JOIN Levels ON Controls_Levels.Level_ID = Levels.ID GO */
new MenuItem
using System; using System.Drawing; using System.Windows.Forms;
Form Menu
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace FormMenu { /// <summary> /// Summary description for FormMenuDemo. /// </summary> public class FormMenuDemo : System.Windows.Forms.Form { private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem mnuFile; private System.Windows.Forms.MenuItem mnuFileExit; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public FormMenuDemo() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (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.mainMenu1 = new System.Windows.Forms.MainMenu(); this.mnuFile = new System.Windows.Forms.MenuItem(); this.mnuFileExit = new System.Windows.Forms.MenuItem(); // // mainMenu1 // this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuFile}); // // mnuFile // this.mnuFile.Index = 0; this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuFileExit}); this.mnuFile.Text = "&File"; // // mnuFileExit // this.mnuFileExit.Index = 0; this.mnuFileExit.Text = "E&xit"; this.mnuFileExit.Click += new System.EventHandler(this.mnuFileExit_Click); // // FormMenuDemo // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Menu = this.mainMenu1; this.Name = "FormMenuDemo"; this.Text = "FormMenuDemo"; } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new FormMenuDemo()); } private void mnuFileExit_Click(object sender, System.EventArgs e) { Application.Exit (); } } }
MdiLayout.TileVertical
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; public class Form1 : Form { private System.Windows.Forms.MenuStrip menuStrip1; private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem newToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem child1ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem windowToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem cascadeToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem tileHorizontalToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem tileVerticalToolStripMenuItem; private System.Windows.Forms.ToolStripSeparator toolStripSeparator1; public Form1() { InitializeComponent(); } private void child1ToolStripMenuItem_Click(object sender, EventArgs e ){ ChildForm formChild = new ChildForm("Child", "winter.jpg" ); formChild.MdiParent = this; formChild.Show(); } private void cascadeToolStripMenuItem_Click(object sender, EventArgs e ) { this.LayoutMdi( MdiLayout.Cascade ); } private void tileHorizontalToolStripMenuItem_Click(object sender, EventArgs e ) { this.LayoutMdi( MdiLayout.TileHorizontal ); } private void tileVerticalToolStripMenuItem_Click(object sender, EventArgs e ) { this.LayoutMdi( MdiLayout.TileVertical ); } private void InitializeComponent() { this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.newToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.child1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.windowToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.cascadeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.tileHorizontalToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.tileVerticalToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator(); this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // // menuStrip1 // this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.fileToolStripMenuItem, this.windowToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.MdiWindowListItem = this.windowToolStripMenuItem; this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.Size = new System.Drawing.Size(353, 24); this.menuStrip1.TabIndex = 1; this.menuStrip1.Text = "menuStrip1"; // // fileToolStripMenuItem // this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.newToolStripMenuItem, this.exitToolStripMenuItem}); this.fileToolStripMenuItem.Name = "fileToolStripMenuItem"; this.fileToolStripMenuItem.Text = "File"; // // newToolStripMenuItem // this.newToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.child1ToolStripMenuItem}); this.newToolStripMenuItem.Name = "newToolStripMenuItem"; this.newToolStripMenuItem.Text = "New"; // // child1ToolStripMenuItem // this.child1ToolStripMenuItem.Name = "child1ToolStripMenuItem"; this.child1ToolStripMenuItem.Text = "Child1"; this.child1ToolStripMenuItem.Click += new System.EventHandler(this.child1ToolStripMenuItem_Click); // // exitToolStripMenuItem // this.exitToolStripMenuItem.Name = "exitToolStripMenuItem"; this.exitToolStripMenuItem.Text = "Exit"; // // windowToolStripMenuItem // this.windowToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.cascadeToolStripMenuItem, this.tileHorizontalToolStripMenuItem, this.tileVerticalToolStripMenuItem, this.toolStripSeparator1}); this.windowToolStripMenuItem.Name = "windowToolStripMenuItem"; this.windowToolStripMenuItem.Text = "Window"; // // cascadeToolStripMenuItem // this.cascadeToolStripMenuItem.Name = "cascadeToolStripMenuItem"; this.cascadeToolStripMenuItem.Text = "Cascade"; this.cascadeToolStripMenuItem.Click += new System.EventHandler(this.cascadeToolStripMenuItem_Click); // // tileHorizontalToolStripMenuItem // this.tileHorizontalToolStripMenuItem.Name = "tileHorizontalToolStripMenuItem"; this.tileHorizontalToolStripMenuItem.Text = "Tile Horizontal"; this.tileHorizontalToolStripMenuItem.Click += new System.EventHandler(this.tileHorizontalToolStripMenuItem_Click); // // tileVerticalToolStripMenuItem // this.tileVerticalToolStripMenuItem.Name = "tileVerticalToolStripMenuItem"; this.tileVerticalToolStripMenuItem.Text = "Tile Vertical"; this.tileVerticalToolStripMenuItem.Click += new System.EventHandler(this.tileVerticalToolStripMenuItem_Click); // // toolStripSeparator1 // this.toolStripSeparator1.Name = "toolStripSeparator1"; // // UsingMDIForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(353, 310); this.Controls.Add(this.menuStrip1); this.IsMdiContainer = true; this.MainMenuStrip = this.menuStrip1; this.Name = "UsingMDIForm"; this.Text = "UsingMDI"; this.menuStrip1.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); } } public class ChildForm : Form { private System.Windows.Forms.PictureBox picDisplay; public ChildForm( string title, string fileName ) { InitializeComponent(); Text = title; picDisplay.Image = Image.FromFile(fileName); } private void InitializeComponent() { this.picDisplay = new System.Windows.Forms.PictureBox(); ((System.ComponentModel.ISupportInitialize)(this.picDisplay)).BeginInit(); this.SuspendLayout(); // // picDisplay // this.picDisplay.Location = new System.Drawing.Point(1, 7); this.picDisplay.Name = "picDisplay"; this.picDisplay.Size = new System.Drawing.Size(225, 247); this.picDisplay.TabIndex = 0; this.picDisplay.TabStop = false; // // ChildForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(227, 256); this.Controls.Add(this.picDisplay); this.Name = "ChildForm"; this.Text = "Child"; ((System.ComponentModel.ISupportInitialize)(this.picDisplay)).EndInit(); this.ResumeLayout(false); } }
Maximized MDI window at startup
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace MdiBasic { public class frmContainer : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; public frmContainer() { InitializeComponent(); MdiBasic.frmChild child = new MdiBasic.frmChild(this); child.Show(); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.IsMdiContainer = true; this.Name = "frmContainer"; this.Text = "MDI Basic"; this.WindowState = System.Windows.Forms.FormWindowState.Maximized; } [STAThread] static void Main() { Application.Run(new frmContainer()); } } public class frmChild : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; public frmChild(MdiBasic.frmContainer parent) { InitializeComponent(); this.MdiParent = parent; } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(300,300); this.Text = "frmChild"; } } }
MDI form
/* User Interfaces in C#: Windows Forms and Custom Controls by Matthew MacDonald Publisher: Apress ISBN: 1590590457 */ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace ImpossibleAPI { public class Global { public static Form1 Main1 = new Form1(); public static Form1 Main2 = new Form1(); public static Form2 Child = new Form2(); [STAThread] public static void Main() { Main1.Text = "Parent 2"; Main2.Text = "Parent 1"; Main1.Show(); Main2.Show(); Child.MdiParent = Main2; Child.Show(); System.Windows.Forms.Application.Run(); } } /// <summary> /// Summary description for Form2. /// </summary> public class Form2 : System.Windows.Forms.Form { internal System.Windows.Forms.Button Button3; internal System.Windows.Forms.Button Button2; internal System.Windows.Forms.Button Button1; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public Form2() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(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.Button3 = new System.Windows.Forms.Button(); this.Button2 = new System.Windows.Forms.Button(); this.Button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // Button3 // this.Button3.Location = new System.Drawing.Point(124, 60); this.Button3.Name = "Button3"; this.Button3.Size = new System.Drawing.Size(88, 32); this.Button3.TabIndex = 5; this.Button3.Text = "Become Child of Parent2"; this.Button3.Click += new System.EventHandler(this.Button3_Click); // // Button2 // this.Button2.Location = new System.Drawing.Point(16, 60); this.Button2.Name = "Button2"; this.Button2.Size = new System.Drawing.Size(88, 32); this.Button2.TabIndex = 4; this.Button2.Text = "Become Child of Parent1"; this.Button2.Click += new System.EventHandler(this.Button2_Click); // // Button1 // this.Button1.Location = new System.Drawing.Point(16, 16); this.Button1.Name = "Button1"; this.Button1.Size = new System.Drawing.Size(88, 32); this.Button1.TabIndex = 3; this.Button1.Text = "Become Parent"; this.Button1.Click += new System.EventHandler(this.Button1_Click); // // Form2 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 14); this.ClientSize = new System.Drawing.Size(292, 150); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.Button3, this.Button2, this.Button1}); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.Name = "Form2"; this.Text = "Form2"; this.ResumeLayout(false); } #endregion private void Button1_Click(object sender, System.EventArgs e) { this.Hide(); this.MdiParent = null; this.IsMdiContainer = true; this.Show(); } private void Button2_Click(object sender, System.EventArgs e) { this.Hide(); this.IsMdiContainer = false; this.MdiParent = Global.Main2; this.Show(); } private void Button3_Click(object sender, System.EventArgs e) { this.Hide(); this.IsMdiContainer = false; this.MdiParent = Global.Main1; this.Show(); } } /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (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() { // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(464, 370); this.IsMdiContainer = true; this.Name = "Form1"; this.Text = "Form1"; this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing); this.Load += new System.EventHandler(this.Form1_Load); } #endregion private void Form1_Load(object sender, System.EventArgs e) { } private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { Application.Exit(); } /// <summary> /// The main entry point for the application. /// </summary> } }
MDI Relatives
/* User Interfaces in C#: Windows Forms and Custom Controls by Matthew MacDonald Publisher: Apress ISBN: 1590590457 */ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace MDIRelatives { /// <summary> /// Summary description for MDIRelatives. /// </summary> public class MDIRelatives : System.Windows.Forms.Form { internal System.Windows.Forms.MainMenu MainMenu1; internal System.Windows.Forms.MenuItem MenuItem1; internal System.Windows.Forms.MenuItem mnuCascade; internal System.Windows.Forms.MenuItem mnuTileV; internal System.Windows.Forms.MenuItem mnuTileH; internal System.Windows.Forms.MenuItem mnuMinimizeAll; internal System.Windows.Forms.ImageList imgButtons; internal System.Windows.Forms.ToolBar ToolBar1; internal System.Windows.Forms.ToolBarButton cmdNew; internal System.Windows.Forms.ToolBarButton cmdClose; private System.ComponentModel.IContainer components; public MDIRelatives() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(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.components = new System.ComponentModel.Container(); System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MDIRelatives)); this.MainMenu1 = new System.Windows.Forms.MainMenu(); this.MenuItem1 = new System.Windows.Forms.MenuItem(); this.mnuCascade = new System.Windows.Forms.MenuItem(); this.mnuTileV = new System.Windows.Forms.MenuItem(); this.mnuTileH = new System.Windows.Forms.MenuItem(); this.mnuMinimizeAll = new System.Windows.Forms.MenuItem(); this.imgButtons = new System.Windows.Forms.ImageList(this.components); this.ToolBar1 = new System.Windows.Forms.ToolBar(); this.cmdNew = new System.Windows.Forms.ToolBarButton(); this.cmdClose = new System.Windows.Forms.ToolBarButton(); this.SuspendLayout(); // // MainMenu1 // this.MainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.MenuItem1}); // // MenuItem1 // this.MenuItem1.Index = 0; this.MenuItem1.MdiList = true; this.MenuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuCascade, this.mnuTileV, this.mnuTileH, this.mnuMinimizeAll}); this.MenuItem1.Text = "Window"; // // mnuCascade // this.mnuCascade.Index = 0; this.mnuCascade.Text = "Cascase"; this.mnuCascade.Click += new System.EventHandler(this.mnuCascade_Click); // // mnuTileV // this.mnuTileV.Index = 1; this.mnuTileV.Text = "Tile Vertical"; this.mnuTileV.Click += new System.EventHandler(this.mnuTileV_Click); // // mnuTileH // this.mnuTileH.Index = 2; this.mnuTileH.Text = "Tile Horizontal"; this.mnuTileH.Click += new System.EventHandler(this.mnuTileH_Click); // // mnuMinimizeAll // this.mnuMinimizeAll.Index = 3; this.mnuMinimizeAll.Text = "Minimize All"; this.mnuMinimizeAll.Click += new System.EventHandler(this.mnuMinimizeAll_Click); // // imgButtons // this.imgButtons.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit; this.imgButtons.ImageSize = new System.Drawing.Size(16, 16); this.imgButtons.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imgButtons.ImageStream"))); this.imgButtons.TransparentColor = System.Drawing.Color.Transparent; // // ToolBar1 // this.ToolBar1.Appearance = System.Windows.Forms.ToolBarAppearance.Flat; this.ToolBar1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.ToolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] { this.cmdNew, this.cmdClose}); this.ToolBar1.DropDownArrows = true; this.ToolBar1.ImageList = this.imgButtons; this.ToolBar1.Name = "ToolBar1"; this.ToolBar1.ShowToolTips = true; this.ToolBar1.Size = new System.Drawing.Size(292, 41); this.ToolBar1.TabIndex = 4; this.ToolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.ToolBar1_ButtonClick); // // cmdNew // this.cmdNew.ImageIndex = 0; this.cmdNew.Text = "New"; // // cmdClose // this.cmdClose.ImageIndex = 1; this.cmdClose.Text = "Close"; // // MDIRelatives // this.AutoScaleBaseSize = new System.Drawing.Size(5, 14); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.ToolBar1}); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.IsMdiContainer = true; this.Menu = this.MainMenu1; this.Name = "MDIRelatives"; this.Text = "MDIRelatives"; this.ResumeLayout(false); } #endregion [STAThread] static void Main() { Application.Run(new MDIRelatives()); } private string synchronizedText = "text"; private int mdiCount = 0; private void ToolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) { // Determine which button was clicked. if (e.Button == cmdNew) { // Show a new ChildForm. Child frmChild = new Child(); frmChild.MdiMDIRelatives = this; frmChild.RefreshText(synchronizedText); mdiCount++; frmChild.Text = "MDI Child # " + mdiCount.ToString(); frmChild.Show(); } else if (e.Button == cmdClose) { // Close the active child. this.ActiveMdiChild.Close(); } } public void RefreshChildren(Child sender, string text) { // Store text for use when creating a child form, or if needed later. synchronizedText = text; // Update children. foreach (Child frm in this.MdiChildren) { if (frm != sender) { frm.RefreshText(text); } } } private void mnuMinimizeAll_Click(object sender, System.EventArgs e) { foreach (Form frm in this.MdiChildren) { frm.WindowState = FormWindowState.Minimized; } } private void mnuTileH_Click(object sender, System.EventArgs e) { this.LayoutMdi(MdiLayout.TileHorizontal); } private void mnuTileV_Click(object sender, System.EventArgs e) { this.LayoutMdi(MdiLayout.TileVertical); } private void mnuCascade_Click(object sender, System.EventArgs e) { this.LayoutMdi(MdiLayout.Cascade); } } } //=========================================================== //=========================================================== using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace MDIRelatives { /// <summary> /// Summary description for Child. /// </summary> public class Child : System.Windows.Forms.Form { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public Child() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(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.textBox1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // textBox1 // this.textBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.textBox1.Location = new System.Drawing.Point(8, 8); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(212, 108); this.textBox1.TabIndex = 1; this.textBox1.Text = "textBox1"; this.textBox1.TextChanged += new System.EventHandler(this.TextBox1_TextChanged); // // Child // this.AutoScaleBaseSize = new System.Drawing.Size(5, 14); this.ClientSize = new System.Drawing.Size(228, 126); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.textBox1}); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.Name = "Child"; this.Text = "Child"; this.ResumeLayout(false); } #endregion internal System.Windows.Forms.TextBox textBox1; private bool isUpdating; private void TextBox1_TextChanged(object sender, System.EventArgs e) { if (this.MdiParent != null && !isUpdating) { // The reference to the MDI parent must be converted to the appropriate // form class in order to access the custom RefreshChildren() method. ((Parent)this.MdiParent).RefreshChildren(this, textBox1.Text); } } public void RefreshText(string text) { // Disable the event to prevent an endless string of updates. isUpdating = true; // Update the control. textBox1.Text = text; // Re-enable the event handler. isUpdating = false; } } } MDIRelatives.zip( 38 k)