アクティブなMDI子ウィンドウを取得する

現在アクティブなMDI子ウィンドウを取得するには
ActiveMdiChildプロパティーを参照します。

例ではメニューから【閉じる】が選択されると
現在アクティブなMDI子ウィンドウを閉じます。

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        System.Diagnostics.PerformanceCounter pc = new System.Diagnostics.PerformanceCounter();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // MDI親ウィンドウにする
            this.IsMdiContainer = true;
        }

        private void 開くToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // MDI子ウィンドウを開く
            Form2 form = new Form2();
            form.MdiParent = this;  // 親を指定
            form.Text = "新しいForm:" + this.MdiChildren.Length.ToString();
            form.Show();
        }

        private void 閉じるToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(this.ActiveMdiChild != null)
            {
                this.ActiveMdiChild.Close();
            }
        }
    }
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)