header
CodePanic! > C#.NET Tips > 今ここ

■アクティブな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();
            }
        }
    }
}





Copyright © 2008.07 - shougo suzaki