C# Menu中共享MenuItem
最近碰到個問題,一個MenuItem Add到一個Menu後就會自動去除前一個menu中的引用。因為在Add Menuitem的過程中會呼叫SetOwner內部函式
internal void SetOwner(ToolStrip newOwner)
{
if (this.owner != newOwner)
{
Font font = this.Font;
this.owner = newOwner;
if (newOwner == null)
{
this.ParentInternal = null;
}
if (!this.state[stateDisposing] && !this.IsDisposed)
{
this.OnOwnerChanged(EventArgs.Empty);
if (font != this.Font)
{
this.OnFontChanged(EventArgs.Empty);
}
}
}
}
{
if (this.owner != newOwner)
{
Font font = this.Font;
this.owner = newOwner;
if (newOwner == null)
{
this.ParentInternal = null;
}
if (!this.state[stateDisposing] && !this.IsDisposed)
{
this.OnOwnerChanged(EventArgs.Empty);
if (font != this.Font)
{
this.OnFontChanged(EventArgs.Empty);
}
}
}
}
如果要在一個程式中幾個menu用到很多同樣的MenuItem怎麼辦呢?
一個方法是Clone每一個MenuItem,然後Add到Menu中。
另外一個方法就是利用Menu的opeing和closing事件來達到共享同一MenuItem的目的
初始化MenuItem
this.MenuItem1= new ToolStripMenuItem();
this.MenuItem2= new ToolStripMenuItem();
this.ContextMenu1 = new ContextMenuStrip();
this.ContextMenu2 = new ContextMenuStrip();
this.ContextMenu1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.MenuItem1,
this.MenuItem2
});
this.MenuItem2= new ToolStripMenuItem();
this.ContextMenu1 = new ContextMenuStrip();
this.ContextMenu2 = new ContextMenuStrip();
this.ContextMenu1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.MenuItem1,
this.MenuItem2
});
掛上事件
this.ContextMenu2.Opening += new CancelEventHandler(this.ContextMenu2_Opening);
this.ContextMenu2.Closing += new ToolStripDropDownClosingEventHandler(this.ContextMenu2_Closing);
this.ContextMenu2.Closing += new ToolStripDropDownClosingEventHandler(this.ContextMenu2_Closing);
動態載入選單
private void ContextMenu2_Opening(object sender, CancelEventArgs e)
{
this.ContextMenu2.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.MenuItem1,
this.MenuItem2
});
}
private void ContextMenu2_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
this.ContextMenu1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.MenuItem1,
this.MenuItem2
});
}
{
this.ContextMenu2.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.MenuItem1,
this.MenuItem2
});
}
private void ContextMenu2_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
this.ContextMenu1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.MenuItem1,
this.MenuItem2
});
}
這裡只是簡單演示了MenuItem的共享方法,靈活使用這個方法基本可以解決多個MenuItem在Menu中的共享問題
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-608034/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- c# winform之選單menu_menuitem_mainmenu_etcC#ORMUIAI
- 動態改變actionbar的menu選單MenuItem的顯示UI
- WPF MenuItem behavior MVVMUIMVVM
- Android-Menu [使用C# And Java實現]AndroidC#Java
- Android -- Options Menu,Context Menu,Popup MenuAndroidContext
- iview 升級指南 —— MenuItem 篇ViewUI
- WPF ListBox DataTemplate MenuItem MVVMUIMVVM
- C# 和 JavaScript Cookie 共享C#JavaScriptCookie
- tkinter中menu選單控制元件(十二)控制元件
- Bootstrap Mega Menuboot
- css dropdown menuCSS
- Ex-menu
- Ultraedit Clipboards (Edit Menu)
- Hide the User MenuIDE
- Android 最簡單的自定義MenuItem之一AndroidUI
- android的Menu使用Android
- menu.lst詳解
- 實戰Struts-Menu
- Tkinter (10) 選單部件 Menu
- Bookmarks Menu (mac書籤管理)Mac
- Qt Application Menu In Window and MacQTAPPMac
- EditorWindow Custom Context MenuContext
- Use windows batch script to create menuWindowsBAT
- Clean context menu under MacOSXContextMac
- Struts-menu原始碼分析原始碼
- How to modify multiple items in the SAP menu
- How To Delete Unwanted Boot Menu Itemsdeleteboot
- Android ActionBar中Overflow Menu(溢位選單)中的一些問題Android
- ASP:Menu 在IE8中顯示不正確的解決方法
- Wise Menu for MacFinder擴充套件程式Mac套件
- 鴻蒙HarmonyOS實戰-ArkUI元件(Menu)鴻蒙UI元件
- ExtJS2.0開發與實踐筆記[3]——Ext中的MenuJS筆記
- 微服務中的資料共享微服務
- C#中substringC#
- C#中的集合C#
- C#中的delegateC#
- c#中呼叫ExcelC#Excel
- C#中的MVCC#MVC