Menu 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
警告
Menu is provided for binary compatibility with .NET Framework and is not intended to be used directly from your code. Use ToolStripDropDown and ToolStripDropDownMenu instead.
代表所有選單的基本功能。
此課程在 .NET Core 3.1 及更新版本中無法使用。 使用ToolStripDropDown和ToolStripDropDownMenu取代並擴展控制。Menu
public ref class Menu abstract : System::ComponentModel::Component
[System.ComponentModel.ListBindable(false)]
public abstract class Menu : System.ComponentModel.Component
[System.ComponentModel.ListBindable(false)]
[System.ComponentModel.Browsable(false)]
[System.Obsolete("`Menu` is provided for binary compatibility with .NET Framework and is not intended to be used directly from your code. Use `ToolStripDropDown` and `ToolStripDropDownMenu` instead.", false, DiagnosticId="WFDEV006", UrlFormat="https://aka.ms/winforms-warnings/{0}")]
public abstract class Menu : System.ComponentModel.Component
[<System.ComponentModel.ListBindable(false)>]
type Menu = class
inherit Component
[<System.ComponentModel.ListBindable(false)>]
[<System.ComponentModel.Browsable(false)>]
[<System.Obsolete("`Menu` is provided for binary compatibility with .NET Framework and is not intended to be used directly from your code. Use `ToolStripDropDown` and `ToolStripDropDownMenu` instead.", false, DiagnosticId="WFDEV006", UrlFormat="https://aka.ms/winforms-warnings/{0}")>]
type Menu = class
inherit Component
Public MustInherit Class Menu
Inherits Component
- 繼承
- 衍生
- 屬性
範例
以下程式碼範例使用派生類別 MenuItem,來建立表單的選單結構。 範例程式碼會新增 a MenuItem 來表示頂層選單項目,加入一個子選單項目用於選擇字型大小,然後在該選單項目中加入兩個子選單項目,分別代表應用程式中的大字型與小字型選擇。 範例要求有一個 MainMenu 名為 mainMenu1 的物件,以及四個 MenuItem 名為 menuItem1、 menuItem2、 menuItem3和 menuItem4的物件。
public:
void CreateMyMenu()
{
// Set the caption for the top-level menu item.
menuItem1->Text = "Edit";
// Set the caption for the first submenu.
menuItem2->Text = "Font Size";
// Set the caption for menuItem2's first submenu.
menuItem3->Text = "Small";
// Set the checked property to true since this is the default value.
menuItem3->Checked = true;
// Define a shortcut key combination for the menu item.
menuItem3->Shortcut = Shortcut::CtrlS;
// Set the caption of the second sub menu item of menuItem2.
menuItem4->Text = "Large";
// Define a shortcut key combination for the menu item.
menuItem4->Shortcut = Shortcut::CtrlL;
// Set the index of the menu item so it is placed below the first submenu item.
menuItem4->Index = 1;
// Add menuItem3 and menuItem4 to menuItem2's list of menu items.
menuItem2->MenuItems->Add( menuItem3 );
menuItem2->MenuItems->Add( menuItem4 );
// Add menuItem2 to menuItem1's list of menu items.
menuItem1->MenuItems->Add( menuItem2 );
// Add menuItem1 to the MainMenu for displaying.
mainMenu1->MenuItems->Add( menuItem1 );
}
public void CreateMyMenu()
{
// Set the caption for the top-level menu item.
menuItem1.Text = "Edit";
// Set the caption for the first submenu.
menuItem2.Text = "Font Size";
// Set the caption for menuItem2's first submenu.
menuItem3.Text = "Small";
// Set the checked property to true since this is the default value.
menuItem3.Checked = true;
// Define a shortcut key combination for the menu item.
menuItem3.Shortcut = Shortcut.CtrlS;
// Set the caption of the second sub menu item of menuItem2.
menuItem4.Text = "Large";
// Define a shortcut key combination for the menu item.
menuItem4.Shortcut = Shortcut.CtrlL;
// Set the index of the menu item so it is placed below the first submenu item.
menuItem4.Index = 1;
// Add menuItem3 and menuItem4 to menuItem2's list of menu items.
menuItem2.MenuItems.Add(menuItem3);
menuItem2.MenuItems.Add(menuItem4);
// Add menuItem2 to menuItem1's list of menu items.
menuItem1.MenuItems.Add(menuItem2);
// Add menuItem1 to the MainMenu for displaying.
mainMenu1.MenuItems.Add(menuItem1);
}
Public Sub CreateMyMenu()
' Set the caption for the top-level menu item.
menuItem1.Text = "Edit"
' Set the caption for the first submenu.
menuItem2.Text = "Font Size"
' Set the caption for menuItem2's first submenu.
menuItem3.Text = "Small"
' Set the checked property to true since this is the default value.
menuItem3.Checked = True
' Define a shortcut key combination for the menu item.
menuItem3.Shortcut = Shortcut.CtrlS
' Set the caption of the second sub menu item of menuItem2.
menuItem4.Text = "Large"
' Define a shortcut key combination for the menu item.
menuItem4.Shortcut = Shortcut.CtrlL
' Set the index of the menu item so it is placed below the first submenu item.
menuItem4.Index = 1
' Add menuItem3 and menuItem4 to menuItem2's list of menu items.
menuItem2.MenuItems.Add(menuItem3)
menuItem2.MenuItems.Add(menuItem4)
' Add menuItem2 to menuItem1's list of menu items.
menuItem1.MenuItems.Add(menuItem2)
' Add menuItem1 to the MainMenu for displaying.
mainMenu1.MenuItems.Add(menuItem1)
End Sub
備註
此課程在 .NET Core 3.1 及更新版本中無法使用。 請改用 ToolStripDropDown 和 ToolStripDropDownMenu。
此類別是 、 、 MainMenu和 MenuItem 類別的基底類別ContextMenu。 你無法建立這個類別的實例。 應用程式的選單由 MenuItem 物件組成。 這些物件可以包含其他 MenuItem 物件,代表子選單項目。 這些 MenuItem 物件可以儲存 MainMenu 在一個中,作為完整的選單結構,作為表單或用來顯示捷徑選單的結構 ContextMenu 。 此類別提供所有選單類別共有的功能。
與許多基底類別不同,該 Menu 類別使用其衍生類別來定義許多屬性。 如果你在多文件介面(MDI)應用程式中使用選單,你可以使用屬性 MdiListItem 指定 a MenuItem ,顯示應用程式中已開啟的 MDI 子表單清單。 該 MenuItems 屬性包含一份儲存在選單類別中的物件清單 MenuItem 。 對於 MainMenu 或 ContextMenu,此性質包含所有 MenuItem 顯示的物件。 對於 MenuItem,屬性 MenuItems 代表與之相關的子選單項目。
除了所有衍生選單類別所提供的屬性外,該 Menu 類別還提供方法, CloneMenu 例如和 MergeMenu,讓你能從現有選單建立新選單,並合併兩個選單結構。
該 Menu 類別也定義了巢狀類別 Menu.MenuItemCollection。 此類別定義了該MenuItems屬性所使用的物件集合MenuItem。 你可以利用類別 Menu.MenuItemCollection 的方法來新增或移除選單項目, MainMenu從 、 ContextMenu或 MenuItem。
建構函式
| 名稱 | Description |
|---|---|
| Menu(MenuItem[]) |
已淘汰.
初始化 Menu 類別的新執行個體。 |
欄位
| 名稱 | Description |
|---|---|
| FindHandle |
已淘汰.
指定 FindMenuItem(Int32, IntPtr) 方法應搜尋一個句柄。 |
| FindShortcut |
已淘汰.
指定 FindMenuItem(Int32, IntPtr) 方法應該搜尋捷徑。 |
屬性
| 名稱 | Description |
|---|---|
| CanRaiseEvents |
已淘汰.
會得到一個值,表示該元件是否能引發事件。 (繼承來源 Component) |
| Container |
已淘汰.
得到 IContainer 包含 Component的 。 (繼承來源 Component) |
| DesignMode |
已淘汰.
會得到一個值,表示目前 Component 是否處於設計模式。 (繼承來源 Component) |
| Events |
已淘汰.
會取得與此 Component連結的事件處理程序清單。 (繼承來源 Component) |
| Handle |
已淘汰.
會得到一個代表選單視窗柄的值。 |
| IsParent |
已淘汰.
會有一個值,表示此菜單是否包含任何菜單項目。 這個屬性是唯讀的。 |
| MdiListItem |
已淘汰.
會獲得一個值,表示 , MenuItem 用來顯示多文件介面(MDI)子表單的清單。 |
| MenuItems |
已淘汰.
會獲得一個值,表示與選單相關的物件集合 MenuItem 。 |
| Name |
已淘汰.
取得或設定 的名稱。Menu |
| Site |
已淘汰.
(繼承來源 Component)
|
| Tag |
已淘汰.
取得或設定與控制項相關的使用者定義資料。 |
方法
事件
| 名稱 | Description |
|---|---|
| Disposed |
已淘汰.
當元件被呼叫方法 Dispose() 時會發生。 (繼承來源 Component) |