ポップアップパネル

ポップアップパネルの作り方。

ポップアップパネルは、

  • パネル外をクリックすると閉じる。
  • 表示してもフォーカスは親フォームに残る。

というもの。手順は、

  1. 中身として表示するコントロールを含む ToolStripControlHost を作成し、
  2. ToolStripDropDown を作成して、そこに作成した ToolStripControlHost を追加し、
  3. ToolStripDropDown.Show() を呼ぶ。

サンプルプログラムは、

using System;
using System.Windows.Forms;

namespace PopupPanelSample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var label = new Label();
            label.Text = "パネルの中味";
            ShowPopupPanel(label);
        }

        private void ShowPopupPanel(Control contents)
        {
            var toolStripControlHost = new ToolStripControlHost(contents);
            var toolStripDropDown = new ToolStripDropDown();
            toolStripDropDown.Items.Add(toolStripControlHost);
            toolStripDropDown.Show(Cursor.Position);
        }
    }
}

表示は、こんなふうになる。

f:id:tt195361:20150512102128p:plain