当前位置:   article > 正文

C#父窗体中创建子窗体,显示、排列子窗体_c#窗体菜单显示窗体

c#窗体菜单显示窗体

目录

 一、创建父窗体Form1

 二、创建子窗体Form2,并在Form1中显示

1、首先在Form1.cs的Form1_Load()中增加代码段  

2、其次还要修改Form1.Designer.cs的窗体设计生成器

(1)修改前

(2)修改后

(3)生成结果:在父窗体中打开所有子窗体

 三、排列子窗体

1、先在Form1窗体中,添加一个MenuStip控件

2、窗体设计生成器内容的自动变化

3、激活控件菜单项的Click事件

(1)鼠标点击控件菜单项“水平平铺”

(2)鼠标依次点击控件菜单项“垂直平铺”、“层叠排列”

(3)添加点击事件属性

(4)运行生成

 一、创建父窗体Form1

        VS中选择C#的Windows窗体应用创建父窗体Form1,默认生成Program.cs、Form1.cs、Form1.Designer.cs三个C#文件,右键Form1.cs有两种显示模式,一种为查看代码Form1.cs,一种为查看设计器Form1.cs[设计]。查看设计器Form1.cs[设计],右键属性,把属性IsMdiContainer的值修改为True。

 二、创建子窗体Form2,并在Form1中显示

1、首先在Form1.cs的Form1_Load()中增加代码段  

        在资源管理器中,右键项目(比如App1),添加窗体(Windows窗体),生成窗体Form2。

以此类推可以生成窗体Form3、Form4。

        在父窗体Form1中实现加载Form1时自动加载子窗体Form2,需要在Form1.cs的Form1_Load()中增加代码段:

  1. Form2 frm2 = new()
  2. {
  3. MdiParent = this
  4. };
  5. frm2.Show();

        以此类推,可以自动加载子窗体Form3、Form4。

  1. //在主窗体中创建子窗体,并在主窗体打开时自动加载子窗体
  2. namespace App1
  3. {
  4. public partial class Form1 : Form
  5. {
  6. public Form1()
  7. {
  8. InitializeComponent();
  9. }
  10. private void Form1_Load(object sender, EventArgs e)
  11. {
  12. Form2 frm2 = new()//自动加载Form2
  13. {
  14. MdiParent = this
  15. };
  16. frm2.Show();
  17. Form3 frm3 = new()//自动加载Form3
  18. {
  19. MdiParent = this
  20. };
  21. frm3.Show();
  22. Form4 frm4 = new()//自动加载Form4
  23. {
  24. MdiParent = this
  25. };
  26. frm4.Show();
  27. }
  28. }
  29. }

2、其次还要修改Form1.Designer.cs的窗体设计生成器

(1)修改前

  1. #region Windows Form Designer generated code
  2. /// <summary>
  3. /// Required method for Designer support - do not modify
  4. /// the contents of this method with the code editor.
  5. /// </summary>
  6. private void InitializeComponent()
  7. {
  8. SuspendLayout();
  9. //
  10. // Form1
  11. //
  12. AutoScaleDimensions = new SizeF(7F, 17F);
  13. AutoScaleMode = AutoScaleMode.Font;
  14. ClientSize = new Size(421, 393);
  15. IsMdiContainer = true;
  16. Name = "Form1";
  17. Text = "Form1";
  18. ResumeLayout(false);
  19. }
  20. #endregion
  21. }
  22. }

(2)修改后

  1. #region Windows Form Designer generated code
  2. /// <summary>
  3. /// Required method for Designer support - do not modify
  4. /// the contents of this method with the code editor.
  5. /// </summary>
  6. private void InitializeComponent()
  7. {
  8. SuspendLayout();
  9. //
  10. // Form1
  11. //
  12. AutoScaleDimensions = new SizeF(7F, 17F);
  13. AutoScaleMode = AutoScaleMode.Font;
  14. ClientSize = new Size(421, 393);
  15. IsMdiContainer = true;
  16. Name = "Form1";
  17. Text = "Form1";
  18. Load += Form1_Load; //在父窗体中显示子窗体,增加
  19. ResumeLayout(false);
  20. }
  21. #endregion

(3)生成结果:在父窗体中打开所有子窗体

 

 三、排列子窗体

        如果一个MDI窗体中有多个子窗体同时打开,假如不对其排列顺序进行调整,那么界面会非常混乱,而且不容易浏览。这时候,可以通过使用带有MdiLayout枚举的LayoutMdi()方法来排列多文档界面父窗体中的子窗体。
  1. 语法如下:
  2. public void LayoutMdi(MdiLayout value)
  3. value:是MdiLayout枚举值之一,用来定义MDI子窗体的布局。
  4. MdiLayout枚举用于指定MDI父窗体中子窗体的布局,其格式:
  5. public enum MdiLayout
  6. MdiLayout的枚举成员如下表:
枚举成员说明
Cascade所有子窗体均层叠在MDI父窗体工作区内
TileHorizontal所有子窗体均水平平铺在MDI父窗体工作区内
TileVertical所有子窗体均垂直平铺在MDI父窗体工作区内

        在上述的实例中继续操作,在Form1窗体中,添加一个MenuStip控件用作该父窗体的菜单项。通过MenuStrip控件建立3个菜单项,分别为“水平平铺”、“垂直平铺”、“层叠排列”。运行程序时通过Form1_Load加载所有子窗体,通过“水平平铺”、“垂直平铺”、“层叠排列”对所有子窗体进行排列。

1、先在Form1窗体中,添加一个MenuStip控件

        打开Form1.cs[设计]→视图→工具箱(或左侧边框的工具箱)→菜单和工具栏→MenuStip→用鼠标拖拽图标到Form1窗体上边框放下→依次根据提示编辑输入菜单项“水平平铺”、“垂直平铺”、“层叠排列”。(为了观察窗体设计生成器内容的变化,鼠标不要点击控件)

 

2、窗体设计生成器内容的自动变化

  1. #region Windows Form Designer generated code
  2. /// <summary>
  3. /// Required method for Designer support - do not modify
  4. /// the contents of this method with the code editor.
  5. /// </summary>
  6. private void InitializeComponent()
  7. {
  8. menuStrip1 = new MenuStrip();
  9. 水平平铺ToolStripMenuItem = new ToolStripMenuItem();
  10. 垂直平铺ToolStripMenuItem = new ToolStripMenuItem();
  11. 层叠排列ToolStripMenuItem = new ToolStripMenuItem();
  12. menuStrip1.SuspendLayout();
  13. SuspendLayout();
  14. //
  15. // menuStrip1
  16. //
  17. menuStrip1.Items.AddRange(new ToolStripItem[] { 水平平铺ToolStripMenuItem, 垂直平铺ToolStripMenuItem, 层叠排列ToolStripMenuItem });
  18. menuStrip1.Location = new Point(0, 0);
  19. menuStrip1.Name = "menuStrip1";
  20. menuStrip1.Size = new Size(484, 25);
  21. menuStrip1.TabIndex = 1;
  22. menuStrip1.Text = "menuStrip1";
  23. //
  24. // 水平平铺ToolStripMenuItem
  25. //
  26. 水平平铺ToolStripMenuItem.Name = "水平平铺ToolStripMenuItem";
  27. 水平平铺ToolStripMenuItem.Size = new Size(68, 21);
  28. 水平平铺ToolStripMenuItem.Text = "水平平铺";
  29. //
  30. // 垂直平铺ToolStripMenuItem
  31. //
  32. 垂直平铺ToolStripMenuItem.Name = "垂直平铺ToolStripMenuItem";
  33. 垂直平铺ToolStripMenuItem.Size = new Size(68, 21);
  34. 垂直平铺ToolStripMenuItem.Text = "垂直平铺";
  35. //
  36. // 层叠排列ToolStripMenuItem
  37. //
  38. 层叠排列ToolStripMenuItem.Name = "层叠排列ToolStripMenuItem";
  39. 层叠排列ToolStripMenuItem.Size = new Size(68, 21);
  40. 层叠排列ToolStripMenuItem.Text = "层叠排列";
  41. //
  42. // Form1
  43. //
  44. AutoScaleDimensions = new SizeF(7F, 17F);
  45. AutoScaleMode = AutoScaleMode.Font;
  46. ClientSize = new Size(484, 461);
  47. Controls.Add(menuStrip1);
  48. IsMdiContainer = true;
  49. MainMenuStrip = menuStrip1;
  50. Name = "Form1";
  51. SizeGripStyle = SizeGripStyle.Hide;
  52. Text = "Form1";
  53. Load += Form1_Load;
  54. menuStrip1.ResumeLayout(false);
  55. menuStrip1.PerformLayout();
  56. ResumeLayout(false);
  57. PerformLayout();
  58. }
  59. #endregion
  60. private MenuStrip menuStrip1;
  61. private ToolStripMenuItem 水平平铺ToolStripMenuItem;
  62. private ToolStripMenuItem 垂直平铺ToolStripMenuItem;
  63. private ToolStripMenuItem 层叠排列ToolStripMenuItem;
  64. }
  65. }

3、激活控件菜单项的Click事件

(1)鼠标点击控件菜单项“水平平铺”

        观察窗体设计生成器自动新增内容和Form1.cs自动新增的内容。

  1. //Form1.cs
  2. namespace App3
  3. {
  4. public partial class Form1 : Form
  5. {
  6. public Form1()
  7. {
  8. InitializeComponent();
  9. }
  10. //在Form1_Load()中显示所有子窗体
  11. private void Form1_Load(object sender, EventArgs e)
  12. {
  13. Form2 frm2 = new()//自动加载Form2
  14. {
  15. MdiParent = this
  16. };
  17. frm2.Show();
  18. Form3 frm3 = new()//自动加载Form3
  19. {
  20. MdiParent = this
  21. };
  22. frm3.Show();
  23. Form4 frm4 = new()//自动加载Form4
  24. {
  25. MdiParent = this
  26. };
  27. frm4.Show();
  28. }
  29. //激活菜单项“水平平铺”的Click事件
  30. private void 水平平铺ToolStripMenuItem_Click(object sender, EventArgs e)
  31. {
  32. }
  33. }
  34. }
  1. //Form1.Designer.cs
  2. namespace App3
  3. {
  4. partial class Form1
  5. {
  6. /// <summary>
  7. /// Required designer variable.
  8. /// </summary>
  9. private System.ComponentModel.IContainer components = null;
  10. /// <summary>
  11. /// Clean up any resources being used.
  12. /// </summary>
  13. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  14. protected override void Dispose(bool disposing)
  15. {
  16. if (disposing && (components != null))
  17. {
  18. components.Dispose();
  19. }
  20. base.Dispose(disposing);
  21. }
  22. #region Windows Form Designer generated code
  23. /// <summary>
  24. /// Required method for Designer support - do not modify
  25. /// the contents of this method with the code editor.
  26. /// </summary>
  27. private void InitializeComponent()
  28. {
  29. menuStrip1 = new MenuStrip();
  30. 水平平铺ToolStripMenuItem = new ToolStripMenuItem();
  31. 垂直平铺ToolStripMenuItem = new ToolStripMenuItem();
  32. 层叠排列ToolStripMenuItem = new ToolStripMenuItem();
  33. menuStrip1.SuspendLayout();
  34. SuspendLayout();
  35. //
  36. // menuStrip1
  37. //
  38. menuStrip1.Items.AddRange(new ToolStripItem[] { 水平平铺ToolStripMenuItem, 垂直平铺ToolStripMenuItem, 层叠排列ToolStripMenuItem });
  39. menuStrip1.Location = new Point(0, 0);
  40. menuStrip1.Name = "menuStrip1";
  41. menuStrip1.Size = new Size(484, 25);
  42. menuStrip1.TabIndex = 1;
  43. menuStrip1.Text = "menuStrip1";
  44. //
  45. // 水平平铺ToolStripMenuItem
  46. //
  47. 水平平铺ToolStripMenuItem.Name = "水平平铺ToolStripMenuItem";
  48. 水平平铺ToolStripMenuItem.Size = new Size(68, 21);
  49. 水平平铺ToolStripMenuItem.Text = "水平平铺";
  50. 水平平铺ToolStripMenuItem.Click += 水平平铺ToolStripMenuItem_Click;
  51. //
  52. // 垂直平铺ToolStripMenuItem
  53. //
  54. 垂直平铺ToolStripMenuItem.Name = "垂直平铺ToolStripMenuItem";
  55. 垂直平铺ToolStripMenuItem.Size = new Size(68, 21);
  56. 垂直平铺ToolStripMenuItem.Text = "垂直平铺";
  57. //
  58. // 层叠排列ToolStripMenuItem
  59. //
  60. 层叠排列ToolStripMenuItem.Name = "层叠排列ToolStripMenuItem";
  61. 层叠排列ToolStripMenuItem.Size = new Size(68, 21);
  62. 层叠排列ToolStripMenuItem.Text = "层叠排列";
  63. //
  64. // Form1
  65. //
  66. AutoScaleDimensions = new SizeF(7F, 17F);
  67. AutoScaleMode = AutoScaleMode.Font;
  68. ClientSize = new Size(484, 461);
  69. Controls.Add(menuStrip1);
  70. IsMdiContainer = true;
  71. MainMenuStrip = menuStrip1;
  72. Name = "Form1";
  73. SizeGripStyle = SizeGripStyle.Hide;
  74. Text = "Form1";
  75. Load += Form1_Load;
  76. menuStrip1.ResumeLayout(false);
  77. menuStrip1.PerformLayout();
  78. ResumeLayout(false);
  79. PerformLayout();
  80. }
  81. #endregion
  82. private MenuStrip menuStrip1;
  83. private ToolStripMenuItem 水平平铺ToolStripMenuItem;
  84. private ToolStripMenuItem 垂直平铺ToolStripMenuItem;
  85. private ToolStripMenuItem 层叠排列ToolStripMenuItem;
  86. }
  87. }

(2)鼠标依次点击控件菜单项“垂直平铺”、“层叠排列”

        继续观察窗体设计生成器自动新增内容和Form1.cs自动新增的内容。

  1. //Form1.cs
  2. namespace App3
  3. {
  4. public partial class Form1 : Form
  5. {
  6. public Form1()
  7. {
  8. InitializeComponent();
  9. }
  10. //在Form1_Load()中显示所有子窗体
  11. private void Form1_Load(object sender, EventArgs e)
  12. {
  13. Form2 frm2 = new()//自动加载Form2
  14. {
  15. MdiParent = this
  16. };
  17. frm2.Show();
  18. Form3 frm3 = new()//自动加载Form3
  19. {
  20. MdiParent = this
  21. };
  22. frm3.Show();
  23. Form4 frm4 = new()//自动加载Form4
  24. {
  25. MdiParent = this
  26. };
  27. frm4.Show();
  28. }
  29. //激活菜单项“水平平铺”的Click事件
  30. private void 水平平铺ToolStripMenuItem_Click(object sender, EventArgs e)
  31. {
  32. }
  33. //激活菜单项“层叠排列”的Click事件
  34. private void 层叠排列ToolStripMenuItem_Click(object sender, EventArgs e)
  35. {
  36. }
  37. //激活菜单项“垂直平铺”的Click事件
  38. private void 垂直平铺ToolStripMenuItem_Click(object sender, EventArgs e)
  39. {
  40. }
  41. }
  42. }
  1. //Form1.Designer.cs
  2. namespace App3
  3. {
  4. partial class Form1
  5. {
  6. /// <summary>
  7. /// Required designer variable.
  8. /// </summary>
  9. private System.ComponentModel.IContainer components = null;
  10. /// <summary>
  11. /// Clean up any resources being used.
  12. /// </summary>
  13. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  14. protected override void Dispose(bool disposing)
  15. {
  16. if (disposing && (components != null))
  17. {
  18. components.Dispose();
  19. }
  20. base.Dispose(disposing);
  21. }
  22. #region Windows Form Designer generated code
  23. /// <summary>
  24. /// Required method for Designer support - do not modify
  25. /// the contents of this method with the code editor.
  26. /// </summary>
  27. private void InitializeComponent()
  28. {
  29. menuStrip1 = new MenuStrip();
  30. 水平平铺ToolStripMenuItem = new ToolStripMenuItem();
  31. 垂直平铺ToolStripMenuItem = new ToolStripMenuItem();
  32. 层叠排列ToolStripMenuItem = new ToolStripMenuItem();
  33. menuStrip1.SuspendLayout();
  34. SuspendLayout();
  35. //
  36. // menuStrip1
  37. //
  38. menuStrip1.Items.AddRange(new ToolStripItem[] { 水平平铺ToolStripMenuItem, 垂直平铺ToolStripMenuItem, 层叠排列ToolStripMenuItem });
  39. menuStrip1.Location = new Point(0, 0);
  40. menuStrip1.Name = "menuStrip1";
  41. menuStrip1.Size = new Size(484, 25);
  42. menuStrip1.TabIndex = 1;
  43. menuStrip1.Text = "menuStrip1";
  44. //
  45. // 水平平铺ToolStripMenuItem
  46. //
  47. 水平平铺ToolStripMenuItem.Name = "水平平铺ToolStripMenuItem";
  48. 水平平铺ToolStripMenuItem.Size = new Size(68, 21);
  49. 水平平铺ToolStripMenuItem.Text = "水平平铺";
  50. 水平平铺ToolStripMenuItem.Click += 水平平铺ToolStripMenuItem_Click; //激活点击事件
  51. //
  52. // 垂直平铺ToolStripMenuItem
  53. //
  54. 垂直平铺ToolStripMenuItem.Name = "垂直平铺ToolStripMenuItem";
  55. 垂直平铺ToolStripMenuItem.Size = new Size(68, 21);
  56. 垂直平铺ToolStripMenuItem.Text = "垂直平铺";
  57. 垂直平铺ToolStripMenuItem.Click += 垂直平铺ToolStripMenuItem_Click; //激活点击事件
  58. //
  59. // 层叠排列ToolStripMenuItem
  60. //
  61. 层叠排列ToolStripMenuItem.Name = "层叠排列ToolStripMenuItem";
  62. 层叠排列ToolStripMenuItem.Size = new Size(68, 21);
  63. 层叠排列ToolStripMenuItem.Text = "层叠排列";
  64. 层叠排列ToolStripMenuItem.Click += 层叠排列ToolStripMenuItem_Click; //激活点击事件
  65. //
  66. // Form1
  67. //
  68. AutoScaleDimensions = new SizeF(7F, 17F);
  69. AutoScaleMode = AutoScaleMode.Font;
  70. ClientSize = new Size(484, 461);
  71. Controls.Add(menuStrip1);
  72. IsMdiContainer = true;
  73. MainMenuStrip = menuStrip1;
  74. Name = "Form1";
  75. SizeGripStyle = SizeGripStyle.Hide;
  76. Text = "Form1";
  77. Load += Form1_Load;
  78. menuStrip1.ResumeLayout(false);
  79. menuStrip1.PerformLayout();
  80. ResumeLayout(false);
  81. PerformLayout();
  82. }
  83. #endregion
  84. private MenuStrip menuStrip1;
  85. private ToolStripMenuItem 水平平铺ToolStripMenuItem;
  86. private ToolStripMenuItem 垂直平铺ToolStripMenuItem;
  87. private ToolStripMenuItem 层叠排列ToolStripMenuItem;
  88. }
  89. }

(3)添加点击事件属性

        在Form1.cs中添加事件属性,从而完成点击控件后执行的操作。

  1. //Form1.cs
  2. namespace App3
  3. {
  4. public partial class Form1 : Form
  5. {
  6. public Form1()
  7. {
  8. InitializeComponent();
  9. }
  10. //在Form1_Load()中显示所有子窗体
  11. private void Form1_Load(object sender, EventArgs e)
  12. {
  13. Form2 frm2 = new()//自动加载Form2
  14. {
  15. MdiParent = this
  16. };
  17. frm2.Show();
  18. Form3 frm3 = new()//自动加载Form3
  19. {
  20. MdiParent = this
  21. };
  22. frm3.Show();
  23. Form4 frm4 = new()//自动加载Form4
  24. {
  25. MdiParent = this
  26. };
  27. frm4.Show();
  28. }
  29. //激活菜单项“水平平铺”的Click事件
  30. private void 水平平铺ToolStripMenuItem_Click(object sender, EventArgs e)
  31. {
  32. LayoutMdi(MdiLayout.TileHorizontal); //添加点击事件的属性
  33. }
  34. //激活菜单项“层叠排列”的Click事件
  35. private void 层叠排列ToolStripMenuItem_Click(object sender, EventArgs e)
  36. {
  37. LayoutMdi(MdiLayout.Cascade); //添加点击事件的属性
  38. }
  39. //激活菜单项“垂直平铺”的Click事件
  40. private void 垂直平铺ToolStripMenuItem_Click(object sender, EventArgs e)
  41. {
  42. LayoutMdi(MdiLayout.TileVertical); //添加点击事件的属性
  43. }
  44. }
  45. }

(4)运行生成

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/326072
推荐阅读
相关标签
  

闽ICP备14008679号