ContextMenuStrip 和 ToolStrip(工具条) MenuItem ToolStripManager

1.  控件的 应用 ------右键选择

2.  事件

3.  属性中set的应用

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

namespace WindowsFormsApplication1
{
    public enum MergeSample
    {
        None,
        Append,
        InsertInSameLocation,
        InsertInSameLocationPreservingOrder,
        ReplacingItems,
        MatchOnly
    }
    public partial class Form1 : Form
    {
        ContextMenuStrip cmsBase;
        ContextMenuStrip cmsItemsToMerge;

        private string scenarioText;
        public string ScenarioText
        {
            get
            {
                return scenarioText;
            }
            set
            {
                scenarioText = value;
                if(ScenarioTextChanged!=null)
                {
                    ScenarioTextChanged(this, EventArgs.Empty);
                }
            }
        }
        public event EventHandler ScenarioTextChanged;

        private MergeSample currentSample = MergeSample.None;
        public MergeSample CurrentSample
        { get
            {
                return currentSample;
            }
           set
            {
                if(currentSample!=value)
                {
                    bool resetRequired = false;

                    if(currentSample==MergeSample.MatchOnly)
                    {
                        resetRequired = true;
                    }
                    currentSample = value;
                    // Undo previous merge, if any.
                    ToolStripManager.RevertMerge(cmsBase, cmsItemsToMerge);
                    if(resetRequired)
                    {
                        RebuildItemsToMerge();
                    }

                    switch(currentSample)
                    {
                        case MergeSample.None:
                            return;
                        case MergeSample.Append:
                            ScenarioText = "This sample adds items to the end of the list using MergeAdd";
                            ShowAppendSamople();
                            break;
                        case MergeSample.InsertInSameLocation:
                            ScenarioText = "This sample adds items to the middle of the list using MergeAction.Insert.\r\n\r\nNotice here how the items are added in reverse order: four, three, two, one. This is because they all have the same merge index.\r\n\r\nA typical scenario is adding menu items to the middle or beginning of the menu when some part of the program is activated. ";
                            ShowInsertInSameLocationSample();
                            break;
                        case MergeSample.InsertInSameLocationPreservingOrder:
                            ScenarioText = "This sample is the same as InsertInSameLocation, except the items are added in normal order by increasing the MergeIndex of \"two merged items\" to be 3, \"three merged items\" to be 5, and so on.\r\n  You could also add the original items backwards to the source ContextMenuStrip.";
                            ShowInsertInSameLocationPreservingOrderSample();
                            break;
                        case MergeSample.ReplacingItems:
                            ScenarioText = "This sample replaces a menu item using MergeAction.Replace. Use this for the MDI scenario where saving does something completely different.\r\n\r\nMatching is based on the Text property. If there is no text match, merging reverts to MergeIndex.";
                            ShowReplaceSample();
                            break;
                        case MergeSample.MatchOnly:
                            ScenarioText = "This sample adds only the subitems from the child to the target ContextMenuStrip.";
                            ShowMatchOnlySample();
                            break;
                    }

                    //Reapply with the new settings;
                    ToolStripManager.Merge(cmsItemsToMerge, cmsBase);
                }
            }
        }

        private void ShowMatchOnlySample()
        {
           foreach(ToolStripMenuItem item in cmsItemsToMerge.Items)
            {
                item.MergeAction = MergeAction.MatchOnly;
                item.DropDownItems.Add("subitem from \"" + item.Text + " " + item.ShortcutKeyDisplayString+ "\"");

            }
        }

        private void ShowReplaceSample()
        {
            throw new NotImplementedException();
        }

        private void ShowInsertInSameLocationPreservingOrderSample()
        {
            throw new NotImplementedException();
        }

        private void ShowInsertInSameLocationSample()
        {
            throw new NotImplementedException();
        }

        private void ShowAppendSamople()
        {
            foreach(ToolStripItem item in cmsItemsToMerge.Items)
            {
                item.MergeAction = MergeAction.Append;
            }
        }

        private void RebuildItemsToMerge()
        {
            //This handles cases where the items collection changes for the sample.
            cmsItemsToMerge.SuspendLayout();
            cmsItemsToMerge.Items.Clear();
            cmsItemsToMerge.Items.Add("one");
            cmsItemsToMerge.Items.Add("two");
            cmsItemsToMerge.Items.Add("three");
            cmsItemsToMerge.Items.Add("four");
            // Distinguish the merged items by setting the shortcut display string.
            foreach (ToolStripMenuItem tsmi in cmsItemsToMerge.Items)
            {
                tsmi.ShortcutKeyDisplayString = "Merged Item";
            }
            cmsItemsToMerge.ResumeLayout();
        }

        public Form1()
        {
            InitializeComponent();
            if(components==null)
            {                                       
                components = new Container();
            }

            cmsBase = new ContextMenuStrip(components);
            cmsItemsToMerge = new ContextMenuStrip(components);

            //cmsBase is the base ContextMenuStrip
            cmsBase.Items.Add("one");
            cmsBase.Items.Add("two");
            cmsBase.Items.Add("three");

            cmsBase.Items.Add("four");

            // cmsItemsToMerge contains the items to merge.
            cmsItemsToMerge.Items.Add("one");
            cmsItemsToMerge.Items.Add("two");
            cmsItemsToMerge.Items.Add("three");
            cmsItemsToMerge.Items.Add("four");

            //Distinguish the merged items by setting the shortcut display string
            foreach(ToolStripMenuItem tsmi in cmsItemsToMerge.Items)
            {
                tsmi.ShortcutKeyDisplayString = "Merged Item";
            }

            // Associate the ContextMenuStrip with the form so that it displays
            // when the user clicks the right mouse button.
            ContextMenuStrip = cmsBase;

            cmsBase.Items[0].Click += new EventHandler(ShowMessageOne_Click);

            CreateCombo();
        }
        
        //ComboBox switching code
        private void CreateCombo()
        {
            //This ComboBox allows the user to switch between the samples
            ComboBox sampleSelectorCombo = new ComboBox();
            sampleSelectorCombo.DataSource = Enum.GetValues(typeof(MergeSample));
            sampleSelectorCombo.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
            sampleSelectorCombo.Dock = DockStyle.Top;
            this.Controls.Add(sampleSelectorCombo);

            TextBox textBox = new TextBox();
            textBox.Multiline = true;
            textBox.Dock = DockStyle.Left;
            textBox.DataBindings.Add("Text", this, "ScenarioText");
            textBox.ReadOnly = true;
            textBox.Width = 150;
            this.Controls.Add(textBox);
            this.BackColor = ProfessionalColors.MenuStripGradientBegin;
            this.Text = "Right click under selection";
        }

        private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox sampleSelectorCombo = sender as ComboBox;
            if(sampleSelectorCombo.SelectedValue!=null)
            {
                CurrentSample = (MergeSample)sampleSelectorCombo.SelectedValue;
            }
        }

        private void ShowMessageOne_Click(object sender, EventArgs e)
        {
           // MessageBox.Show("one", "Message");
          // MessageBox.Show()

        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {

        }
        //  public void ShowMessageOne(Object )
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值