星期六, 7月 24, 2021

[C#] ContextMenuStrip

根據該篇 ContextMenuStrip 類別 的練習,以 MenuStrip + ContextMenuStrip 為主,不會紀錄 ToolStrip 使用方式,該官方範例會說明 ContextMenuStrip 使用方式,並把 ContextMenuStrip 綁上 MenuStrip 使用
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace ContextMenuStripSample
{
    public partial class Form1 : Form
    {
        private ContextMenuStrip fruitContextMenuStrip;

        public Form1()
        {
            InitializeComponent();

            fruitContextMenuStrip = new ContextMenuStrip();
            fruitContextMenuStrip.Opening += FruitContextMenuStrip_Opening;
            this.ContextMenuStrip = fruitContextMenuStrip;

            ButtonInit();
            MenuStripInit();

        }

        private void ButtonInit()
        {
            Button b = new Button();
            b.Location = new Point(60, 60);
            this.Controls.Add(b);
            b.ContextMenuStrip = fruitContextMenuStrip;
        }

        private void MenuStripInit()
        {
            MenuStrip ms = new MenuStrip();
            ToolStripMenuItem fruitToolStripMenuItem = new ToolStripMenuItem("Fruit", null, null, "Fruit");
            ms.Items.Add(fruitToolStripMenuItem);

            ms.Dock = DockStyle.Top;

            // MenuStrip.Fruit 選項透過 ContextMenuStrip 來顯示
            fruitToolStripMenuItem.DropDown = fruitContextMenuStrip;

            this.Controls.Add(ms);
        }

        private void FruitContextMenuStrip_Opening(object sender, CancelEventArgs e)
        {

            Control ctl = fruitContextMenuStrip.SourceControl as Control;
            // 判斷 ToolStripDropDownItem 會包含 ToolStripMenuItem
            ToolStripDropDownItem tsi = fruitContextMenuStrip.OwnerItem as ToolStripDropDownItem;

            fruitContextMenuStrip.Items.Clear();

            // Check the source control first.
            if (ctl != null)
            {
                fruitContextMenuStrip.Items.Add("右鍵使用 ContextMenuStrip");
                fruitContextMenuStrip.Items.Add("Source:" + ctl.GetType().ToString());
            }
            else if (tsi != null)
            {
                // Add custom item (ToolStripDropDownButton or ToolStripMenuItem)
                fruitContextMenuStrip.Items.Add("點選 MenuStrip");
                fruitContextMenuStrip.Items.Add("Source:" + tsi.GetType().ToString());
            }

            // 原來分隔線可以直接用文字 - 代表
            fruitContextMenuStrip.Items.Add("-");
            fruitContextMenuStrip.Items.Add(new ToolStripSeparator());

            ToolStripMenuItem CustomeItem = new ToolStripMenuItem();
            CustomeItem.Click += Item_Click;

            fruitContextMenuStrip.Items.Add("Custom", null, Item_Click);
            fruitContextMenuStrip.Items.Add("Apples" , null , Item_Click);
            fruitContextMenuStrip.Items.Add("Oranges" , null , Item_Click);
            fruitContextMenuStrip.Items.Add("Pears" , null , Item_Click);

            // Set Cancel to false. It is optimized to true based on empty entry.
            // e.Cancel = true 的話,MenuStrip 的 ContextMenuStrip 就不會顯示啦
            e.Cancel = false;
        }

        private void Item_Click(object sender, EventArgs e)
        {
            MessageBox.Show((sender as ToolStripMenuItem).Text);
        }
    }
}
以前都是每一個 ToolStripMenuItem 都要單獨產生後去綁定 Click 事件,該練習時才發現說,有一個多載方法可以直接傳進去,小發現

[C#] ContextMenuStrip-3

點選 MenuStrip

[C#] ContextMenuStrip-1

使用 ContextMenuStrip

[C#] ContextMenuStrip-2


沒有留言:

張貼留言