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 )
}
}