用户控件
有一个用户控件,里面有一个菜单,菜单中有:保存,退出,新增等项目.分别调用指定FORM中Save(),Exit(),Add()等方法.请问应如何实现?
谢谢
------解决方案--------------------//用ContextMenu实现
public partial class Form1 : Form
{
private System.Windows.Forms.ContextMenu contextMenu_treeview;
private System.Windows.Forms.MenuItem menuItem_save;
private System.Windows.Forms.MenuItem menuItem_exit;
private System.Windows.Forms.MenuItem menuItem_add;
public Form1()
{
InitializeComponent();
//生成treeview快捷菜单
this.menuItem_save= new System.Windows.Forms.MenuItem( "save ", onSave);
//onSave为save的click事件,后面添加的方法相同
this.menuItem_exit= new System.Windows.Forms.MenuItem( "exit ");
this.menuItem_add= new System.Windows.Forms.MenuItem( "add ");
this.contextMenu_treeview = new System.Windows.Forms.ContextMenu();
this.contextMenu_treeview.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]{
menuItem_save,menuItem_exit,menuItem_add});
//假定你说的Control控件为treeView1
this.treeView1.ContextMenu = this.contextMenu_treeview;
}
public void onSave(object sender, EventArgs e)
{
//为事件添加代码
}
}
就这样