Procedura: Creare un form MDI con unione di menu e controlli ToolStrip

Lo spazio dei nomi System.Windows.Forms supporta le applicazioni MDI (Multiple Document Interface, interfaccia a documenti multipli), mentre il controllo MenuStrip supporta l'unione di menu. I form MDI possono inoltre usare i controlli ToolStrip.

In Visual Studio è disponibile un ampio supporto per questa funzionalità.

Vedere anche Procedura dettagliata: creazione di un form con interfaccia a documenti multipli tramite l'unione di menu e i controlli ToolStrip.

Esempio

L'esempio di codice seguente illustra come usare i controlli ToolStripPanel con un form MDI. Il form supporta anche l'unione dei menu con menu figlio.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace MdiFormCS
{
    // This code example demonstrates an MDI form
    // that supports menu merging and moveable
    // ToolStrip controls
    public class Form1 : Form
    {
        private MenuStrip menuStrip1;
        private ToolStripMenuItem toolStripMenuItem1;
        private ToolStripMenuItem newToolStripMenuItem;
        private ToolStripPanel toolStripPanel1;
        private ToolStrip toolStrip1;
        private ToolStripPanel toolStripPanel2;
        private ToolStrip toolStrip2;
        private ToolStripPanel toolStripPanel3;
        private ToolStrip toolStrip3;
        private ToolStripPanel toolStripPanel4;
        private ToolStrip toolStrip4;

        private System.ComponentModel.IContainer components = null;

        public Form1()
        {
            InitializeComponent();
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        // This method creates a new ChildForm instance
        // and attaches it to the MDI parent form.
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ChildForm f = new ChildForm();
            f.MdiParent = this;
            f.Text = "Form - " + this.MdiChildren.Length.ToString();
            f.Show();
        }

        #region Windows Form Designer generated code

        private void InitializeComponent()
        {
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
            this.newToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripPanel1 = new System.Windows.Forms.ToolStripPanel();
            this.toolStrip1 = new System.Windows.Forms.ToolStrip();
            this.toolStripPanel2 = new System.Windows.Forms.ToolStripPanel();
            this.toolStrip2 = new System.Windows.Forms.ToolStrip();
            this.toolStripPanel3 = new System.Windows.Forms.ToolStripPanel();
            this.toolStrip3 = new System.Windows.Forms.ToolStrip();
            this.toolStripPanel4 = new System.Windows.Forms.ToolStripPanel();
            this.toolStrip4 = new System.Windows.Forms.ToolStrip();
            this.menuStrip1.SuspendLayout();
            this.toolStripPanel1.SuspendLayout();
            this.toolStripPanel2.SuspendLayout();
            this.toolStripPanel3.SuspendLayout();
            this.toolStripPanel4.SuspendLayout();
            this.SuspendLayout();
            //
            // menuStrip1
            //
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripMenuItem1});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.MdiWindowListItem = this.toolStripMenuItem1;
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(292, 24);
            this.menuStrip1.TabIndex = 0;
            this.menuStrip1.Text = "menuStrip1";
            //
            // toolStripMenuItem1
            //
            this.toolStripMenuItem1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.newToolStripMenuItem});
            this.toolStripMenuItem1.Name = "toolStripMenuItem1";
            this.toolStripMenuItem1.Size = new System.Drawing.Size(57, 20);
            this.toolStripMenuItem1.Text = "Window";
            //
            // newToolStripMenuItem
            //
            this.newToolStripMenuItem.Name = "newToolStripMenuItem";
            this.newToolStripMenuItem.Size = new System.Drawing.Size(106, 22);
            this.newToolStripMenuItem.Text = "New";
            this.newToolStripMenuItem.Click += new System.EventHandler(this.newToolStripMenuItem_Click);
            //
            // toolStripPanel1
            //
            this.toolStripPanel1.Controls.Add(this.toolStrip1);
            this.toolStripPanel1.Dock = System.Windows.Forms.DockStyle.Left;
            this.toolStripPanel1.Location = new System.Drawing.Point(0, 49);
            this.toolStripPanel1.Name = "toolStripPanel1";
            this.toolStripPanel1.Orientation = System.Windows.Forms.Orientation.Vertical;
            this.toolStripPanel1.RowMargin = new System.Windows.Forms.Padding(0, 3, 0, 0);
            this.toolStripPanel1.Size = new System.Drawing.Size(26, 199);
            //
            // toolStrip1
            //
            this.toolStrip1.Dock = System.Windows.Forms.DockStyle.None;
            this.toolStrip1.Location = new System.Drawing.Point(0, 3);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.Size = new System.Drawing.Size(26, 109);
            this.toolStrip1.TabIndex = 0;
            //
            // toolStripPanel2
            //
            this.toolStripPanel2.Controls.Add(this.toolStrip2);
            this.toolStripPanel2.Dock = System.Windows.Forms.DockStyle.Top;
            this.toolStripPanel2.Location = new System.Drawing.Point(0, 24);
            this.toolStripPanel2.Name = "toolStripPanel2";
            this.toolStripPanel2.Orientation = System.Windows.Forms.Orientation.Horizontal;
            this.toolStripPanel2.RowMargin = new System.Windows.Forms.Padding(3, 0, 0, 0);
            this.toolStripPanel2.Size = new System.Drawing.Size(292, 25);
            //
            // toolStrip2
            //
            this.toolStrip2.Dock = System.Windows.Forms.DockStyle.None;
            this.toolStrip2.Location = new System.Drawing.Point(3, 0);
            this.toolStrip2.Name = "toolStrip2";
            this.toolStrip2.Size = new System.Drawing.Size(109, 25);
            this.toolStrip2.TabIndex = 0;
            //
            // toolStripPanel3
            //
            this.toolStripPanel3.Controls.Add(this.toolStrip3);
            this.toolStripPanel3.Dock = System.Windows.Forms.DockStyle.Right;
            this.toolStripPanel3.Location = new System.Drawing.Point(266, 49);
            this.toolStripPanel3.Name = "toolStripPanel3";
            this.toolStripPanel3.Orientation = System.Windows.Forms.Orientation.Vertical;
            this.toolStripPanel3.RowMargin = new System.Windows.Forms.Padding(0, 3, 0, 0);
            this.toolStripPanel3.Size = new System.Drawing.Size(26, 199);
            //
            // toolStrip3
            //
            this.toolStrip3.Dock = System.Windows.Forms.DockStyle.None;
            this.toolStrip3.Location = new System.Drawing.Point(0, 3);
            this.toolStrip3.Name = "toolStrip3";
            this.toolStrip3.Size = new System.Drawing.Size(26, 109);
            this.toolStrip3.TabIndex = 0;
            //
            // toolStripPanel4
            //
            this.toolStripPanel4.Controls.Add(this.toolStrip4);
            this.toolStripPanel4.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.toolStripPanel4.Location = new System.Drawing.Point(0, 248);
            this.toolStripPanel4.Name = "toolStripPanel4";
            this.toolStripPanel4.Orientation = System.Windows.Forms.Orientation.Horizontal;
            this.toolStripPanel4.RowMargin = new System.Windows.Forms.Padding(3, 0, 0, 0);
            this.toolStripPanel4.Size = new System.Drawing.Size(292, 25);
            //
            // toolStrip4
            //
            this.toolStrip4.Dock = System.Windows.Forms.DockStyle.None;
            this.toolStrip4.Location = new System.Drawing.Point(3, 0);
            this.toolStrip4.Name = "toolStrip4";
            this.toolStrip4.Size = new System.Drawing.Size(109, 25);
            this.toolStrip4.TabIndex = 0;
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Controls.Add(this.toolStripPanel3);
            this.Controls.Add(this.toolStripPanel1);
            this.Controls.Add(this.toolStripPanel2);
            this.Controls.Add(this.menuStrip1);
            this.Controls.Add(this.toolStripPanel4);
            this.IsMdiContainer = true;
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "Form1";
            this.Text = "Form1";
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.toolStripPanel1.ResumeLayout(false);
            this.toolStripPanel1.PerformLayout();
            this.toolStripPanel2.ResumeLayout(false);
            this.toolStripPanel2.PerformLayout();
            this.toolStripPanel3.ResumeLayout(false);
            this.toolStripPanel3.PerformLayout();
            this.toolStripPanel4.ResumeLayout(false);
            this.toolStripPanel4.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        #endregion
    }

    public class ChildForm : Form
    {
        private System.Windows.Forms.MenuStrip menuStrip1;
        private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem1;
        private System.ComponentModel.IContainer components = null;

        public ChildForm()
        {
            InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        private void InitializeComponent()
        {
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
            this.menuStrip1.SuspendLayout();
            this.SuspendLayout();
            //
            // menuStrip1
            //
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripMenuItem1});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(292, 24);
            this.menuStrip1.TabIndex = 0;
            this.menuStrip1.Text = "menuStrip1";
            //
            // toolStripMenuItem1
            //
            this.toolStripMenuItem1.Name = "toolStripMenuItem1";
            this.toolStripMenuItem1.Size = new System.Drawing.Size(90, 20);
            this.toolStripMenuItem1.Text = "ChildMenuItem";
            //
            // ChildForm
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Controls.Add(this.menuStrip1);
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "ChildForm";
            this.Text = "ChildForm";
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        #endregion
    }

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }
    }
}
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms

' This code example demonstrates an MDI form 
' that supports menu merging and moveable 
' ToolStrip controls
Public Class Form1
   Inherits Form
   Private menuStrip1 As MenuStrip
   Private toolStripMenuItem1 As ToolStripMenuItem
   Private WithEvents newToolStripMenuItem As ToolStripMenuItem
   Private toolStripPanel1 As ToolStripPanel
   Private toolStrip1 As ToolStrip
   Private toolStripPanel2 As ToolStripPanel
   Private toolStrip2 As ToolStrip
   Private toolStripPanel3 As ToolStripPanel
   Private toolStrip3 As ToolStrip
   Private toolStripPanel4 As ToolStripPanel
   Private toolStrip4 As ToolStrip
   
   Private components As System.ComponentModel.IContainer = Nothing
   
   Public Sub New()
      InitializeComponent()
    End Sub
   
   Protected Overrides Sub Dispose(disposing As Boolean)
      If disposing AndAlso (components IsNot Nothing) Then
         components.Dispose()
      End If
      MyBase.Dispose(disposing)
    End Sub
   
   
   ' This method creates a new ChildForm instance 
   ' and attaches it to the MDI parent form.
    Private Sub newToolStripMenuItem_Click( _
    ByVal sender As Object, _
    ByVal e As EventArgs) _
    Handles newToolStripMenuItem.Click

        Dim f As New ChildForm()
        f.MdiParent = Me
        f.Text = "Form - " + Me.MdiChildren.Length.ToString()
        f.Show()

    End Sub

   #Region "Windows Form Designer generated code"
   
   Private Sub InitializeComponent()
        Me.menuStrip1 = New System.Windows.Forms.MenuStrip
        Me.toolStripMenuItem1 = New System.Windows.Forms.ToolStripMenuItem
        Me.newToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.toolStripPanel1 = New System.Windows.Forms.ToolStripPanel
        Me.toolStrip1 = New System.Windows.Forms.ToolStrip
        Me.toolStripPanel2 = New System.Windows.Forms.ToolStripPanel
        Me.toolStrip2 = New System.Windows.Forms.ToolStrip
        Me.toolStripPanel3 = New System.Windows.Forms.ToolStripPanel
        Me.toolStrip3 = New System.Windows.Forms.ToolStrip
        Me.toolStripPanel4 = New System.Windows.Forms.ToolStripPanel
        Me.toolStrip4 = New System.Windows.Forms.ToolStrip
        Me.menuStrip1.SuspendLayout()
        Me.toolStripPanel1.SuspendLayout()
        Me.toolStripPanel2.SuspendLayout()
        Me.toolStripPanel3.SuspendLayout()
        Me.toolStripPanel4.SuspendLayout()
        Me.SuspendLayout()
        '
        'menuStrip1
        '
        Me.menuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.toolStripMenuItem1})
        Me.menuStrip1.Location = New System.Drawing.Point(0, 0)
        Me.menuStrip1.MdiWindowListItem = Me.toolStripMenuItem1
        Me.menuStrip1.Name = "menuStrip1"
        Me.menuStrip1.Size = New System.Drawing.Size(292, 24)
        Me.menuStrip1.TabIndex = 0
        Me.menuStrip1.Text = "menuStrip1"
        '
        'toolStripMenuItem1
        '
        Me.toolStripMenuItem1.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.newToolStripMenuItem})
        Me.toolStripMenuItem1.Name = "toolStripMenuItem1"
        Me.toolStripMenuItem1.Size = New System.Drawing.Size(57, 20)
        Me.toolStripMenuItem1.Text = "Window"
        '
        'newToolStripMenuItem
        '
        Me.newToolStripMenuItem.Name = "newToolStripMenuItem"
        Me.newToolStripMenuItem.Size = New System.Drawing.Size(106, 22)
        Me.newToolStripMenuItem.Text = "New"
        '
        'toolStripPanel1
        '
        Me.toolStripPanel1.Controls.Add(Me.toolStrip1)
        Me.toolStripPanel1.Dock = System.Windows.Forms.DockStyle.Left
        Me.toolStripPanel1.Location = New System.Drawing.Point(0, 49)
        Me.toolStripPanel1.Name = "toolStripPanel1"
        Me.toolStripPanel1.Orientation = System.Windows.Forms.Orientation.Vertical
        Me.toolStripPanel1.RowMargin = New System.Windows.Forms.Padding(0, 3, 0, 0)
        Me.toolStripPanel1.Size = New System.Drawing.Size(26, 199)
        '
        'toolStrip1
        '
        Me.toolStrip1.Dock = System.Windows.Forms.DockStyle.None
        Me.toolStrip1.Location = New System.Drawing.Point(0, 3)
        Me.toolStrip1.Name = "toolStrip1"
        Me.toolStrip1.Size = New System.Drawing.Size(26, 109)
        Me.toolStrip1.TabIndex = 0
        '
        'toolStripPanel2
        '
        Me.toolStripPanel2.Controls.Add(Me.toolStrip2)
        Me.toolStripPanel2.Dock = System.Windows.Forms.DockStyle.Top
        Me.toolStripPanel2.Location = New System.Drawing.Point(0, 24)
        Me.toolStripPanel2.Name = "toolStripPanel2"
        Me.toolStripPanel2.Orientation = System.Windows.Forms.Orientation.Horizontal
        Me.toolStripPanel2.RowMargin = New System.Windows.Forms.Padding(3, 0, 0, 0)
        Me.toolStripPanel2.Size = New System.Drawing.Size(292, 25)
        '
        'toolStrip2
        '
        Me.toolStrip2.Dock = System.Windows.Forms.DockStyle.None
        Me.toolStrip2.Location = New System.Drawing.Point(3, 0)
        Me.toolStrip2.Name = "toolStrip2"
        Me.toolStrip2.Size = New System.Drawing.Size(109, 25)
        Me.toolStrip2.TabIndex = 0
        '
        'toolStripPanel3
        '
        Me.toolStripPanel3.Controls.Add(Me.toolStrip3)
        Me.toolStripPanel3.Dock = System.Windows.Forms.DockStyle.Right
        Me.toolStripPanel3.Location = New System.Drawing.Point(266, 49)
        Me.toolStripPanel3.Name = "toolStripPanel3"
        Me.toolStripPanel3.Orientation = System.Windows.Forms.Orientation.Vertical
        Me.toolStripPanel3.RowMargin = New System.Windows.Forms.Padding(0, 3, 0, 0)
        Me.toolStripPanel3.Size = New System.Drawing.Size(26, 199)
        '
        'toolStrip3
        '
        Me.toolStrip3.Dock = System.Windows.Forms.DockStyle.None
        Me.toolStrip3.Location = New System.Drawing.Point(0, 3)
        Me.toolStrip3.Name = "toolStrip3"
        Me.toolStrip3.Size = New System.Drawing.Size(26, 109)
        Me.toolStrip3.TabIndex = 0
        '
        'toolStripPanel4
        '
        Me.toolStripPanel4.Controls.Add(Me.toolStrip4)
        Me.toolStripPanel4.Dock = System.Windows.Forms.DockStyle.Bottom
        Me.toolStripPanel4.Location = New System.Drawing.Point(0, 248)
        Me.toolStripPanel4.Name = "toolStripPanel4"
        Me.toolStripPanel4.Orientation = System.Windows.Forms.Orientation.Horizontal
        Me.toolStripPanel4.RowMargin = New System.Windows.Forms.Padding(3, 0, 0, 0)
        Me.toolStripPanel4.Size = New System.Drawing.Size(292, 25)
        '
        'toolStrip4
        '
        Me.toolStrip4.Dock = System.Windows.Forms.DockStyle.None
        Me.toolStrip4.Location = New System.Drawing.Point(3, 0)
        Me.toolStrip4.Name = "toolStrip4"
        Me.toolStrip4.Size = New System.Drawing.Size(109, 25)
        Me.toolStrip4.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.toolStripPanel3)
        Me.Controls.Add(Me.toolStripPanel1)
        Me.Controls.Add(Me.toolStripPanel2)
        Me.Controls.Add(Me.menuStrip1)
        Me.Controls.Add(Me.toolStripPanel4)
        Me.IsMdiContainer = True
        Me.MainMenuStrip = Me.menuStrip1
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.menuStrip1.ResumeLayout(False)
        Me.menuStrip1.PerformLayout()
        Me.toolStripPanel1.ResumeLayout(False)
        Me.toolStripPanel1.PerformLayout()
        Me.toolStripPanel2.ResumeLayout(False)
        Me.toolStripPanel2.PerformLayout()
        Me.toolStripPanel3.ResumeLayout(False)
        Me.toolStripPanel3.PerformLayout()
        Me.toolStripPanel4.ResumeLayout(False)
        Me.toolStripPanel4.PerformLayout()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub 
   
   #End Region
End Class


Public Class ChildForm
   Inherits Form
   Private menuStrip1 As System.Windows.Forms.MenuStrip
   Private toolStripMenuItem1 As System.Windows.Forms.ToolStripMenuItem
   Private components As System.ComponentModel.IContainer = Nothing
   
   
   Public Sub New()
      InitializeComponent()
    End Sub
   
   
   Protected Overrides Sub Dispose(disposing As Boolean)
      If disposing AndAlso (components IsNot Nothing) Then
         components.Dispose()
      End If
      MyBase.Dispose(disposing)
    End Sub
   
   #Region "Windows Form Designer generated code"
  
   Private Sub InitializeComponent()
      Me.menuStrip1 = New System.Windows.Forms.MenuStrip()
      Me.toolStripMenuItem1 = New System.Windows.Forms.ToolStripMenuItem()
      Me.menuStrip1.SuspendLayout()
      Me.SuspendLayout()
      ' 
      ' menuStrip1
      ' 
      Me.menuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.toolStripMenuItem1})
      Me.menuStrip1.Location = New System.Drawing.Point(0, 0)
      Me.menuStrip1.Name = "menuStrip1"
      Me.menuStrip1.Size = New System.Drawing.Size(292, 24)
      Me.menuStrip1.TabIndex = 0
      Me.menuStrip1.Text = "menuStrip1"
      ' 
      ' toolStripMenuItem1
      ' 
      Me.toolStripMenuItem1.Name = "toolStripMenuItem1"
      Me.toolStripMenuItem1.Size = New System.Drawing.Size(90, 20)
      Me.toolStripMenuItem1.Text = "ChildMenuItem"
      ' 
      ' ChildForm
      ' 
      Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
      Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
      Me.ClientSize = New System.Drawing.Size(292, 273)
      Me.Controls.Add(menuStrip1)
      Me.MainMenuStrip = Me.menuStrip1
      Me.Name = "ChildForm"
      Me.Text = "ChildForm"
      Me.menuStrip1.ResumeLayout(False)
      Me.menuStrip1.PerformLayout()
      Me.ResumeLayout(False)
      Me.PerformLayout()
   End Sub 
   
   #End Region
End Class


Public Class Program

    ' The main entry point for the application.
    <STAThread()> _
    Shared Sub Main()
        Application.EnableVisualStyles()
        Application.Run(New Form1())
    End Sub
End Class

Compilazione del codice

L'esempio presenta i requisiti seguenti:

  • Riferimenti agli assembly System.Drawing e System.Windows.Forms.

Vedi anche