public partial class Form1 : Form
{
private ToolStripUserControl toolStripUC = new ToolStripUserControl();
public Form1()
{
InitializeComponent();
// Assigns an existing ContextMenuStrip, to the Form or any other Control
this.ContextMenuStrip = contextMenuStrip1;
// Inserts the ToolStripUserControl and a Separator
contextMenuStrip1.Items.Insert(0, new ToolStripSeparator());
contextMenuStrip1.Items.Insert(0, toolStripUC);
// Subscribes to the ButtonAction event
toolStripUC.ButtonAction += (s, e)
=> { this.Text = $"Your Action is {e.UserAction}, triggered by {e.TriggerControl.Name}"; };
contextMenuStrip1.Closed += (s, e) => this.Text = "Right-Click on me";
}
}
字符串
ToolStripControlHost派生类:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
[ToolboxItem(false)]
public class ToolStripUserControl : ToolStripControlHost
{
// Pass the UserControl, MenuStripNavigationBar, to the base class.
public ToolStripUserControl() : base(new MenuStripNavigationBar()) { }
public MenuStripNavigationBar NavigatorBar => Control as MenuStripNavigationBar;
// Exposes the ButtonActions Dictionary, to redefine the Buttons' actions
public Dictionary<Control, ButtonAction> ButtonActions {
get => NavigatorBar.ButtonActions;
set => NavigatorBar.ButtonActions = value;
}
// Subscribe to the events you want to expose...
protected override void OnSubscribeControlEvents(Control ctl)
{
base.OnSubscribeControlEvents(ctl);
var navigatorBar = (MenuStripNavigationBar)ctl;
navigatorBar.UserAction += OnButtonAction;
}
// ...and then unsubscribe. This is called when the Form is destroyed
protected override void OnUnsubscribeControlEvents(Control ctl)
{
base.OnUnsubscribeControlEvents(ctl);
var navigatorBar = (MenuStripNavigationBar)ctl;
navigatorBar.UserAction -= OnButtonAction;
}
// Exposes a public custom event
public event EventHandler<UserActionArgs> ButtonAction;
// Raises the event when an UserAction is triggered
private void OnButtonAction(object sender, EventArgs e)
{
var ctl = sender as Control;
var userAction = new UserActionArgs(ButtonActions[ctl], ctl);
ButtonAction?.Invoke(this, userAction);
}
}
型
自定义EventArgs对象:
public class UserActionArgs : EventArgs
{
public UserActionArgs(ButtonAction action, Control control)
{
this.UserAction = action;
this.TriggerControl = control;
}
public ButtonAction UserAction { get; }
public Control TriggerControl { get; }
}
型
Actions枚举器:
public enum ButtonAction
{
MoveBack,
MoveForward,
PlayMusic,
PlayAnimation
}
1条答案
按热度按时间moiiocjp1#
这是一个自定义组件,继承了ToolStripControlHost(如.Net的ToolStripTextBox,ToolStripComboBox等),它承载了一个UserControl而不是标准Control。
▶构建一个UserControl,你可以添加任何其他控件到它的表面,并像往常一样管理它们的操作,除了UserControl的事件通过
ToolStripControlHost
派生对象公开,所以实现者不需要知道任何关于底层托管控件的信息。您要公开的宿主控件的事件订阅覆盖OnSubscribeControlEvents()(当然也取消订阅覆盖OnUnsubscribeControlEvents),然后引发一个相关或 * 合成 * 事件,该事件根据需要返回值。
▶这在
ToolStripControlHost
类中显示,请参阅public event EventHandler<UserActionArgs> ButtonAction
事件,该事件返回自定义EventArgs
参数,该参数包括确定所单击的Button执行的操作所需的元素。当需要时,宿主控件通过ToolStripControlHost.Control属性公开。
您可以在使用它的Form的构造函数中初始化现有的ContextMenuStrip(
contextMenuStrip1
,这里):字符串
ToolStripControlHost
派生类:型
自定义EventArgs对象:
型
Actions枚举器:
型
它是这样工作的:
的数据
这是完整的UserControl,以简化测试:
型
设计器文件:
型