以前作成した MenuItem を現在のメニューの末尾に追加します。
Overloads Public Overridable Function Add( _
ByVal item As MenuItem _) As Integer
[C#]
public virtual int Add(MenuItemitem);
[C++]
public: virtual int Add(MenuItem* item);
[JScript]
public function Add(
item : MenuItem) : int;
パラメータ
- item
追加する MenuItem 。
戻り値
コレクション内の項目が格納されている位置を示す 0 から始まるインデックス番号。
解説
MenuItem は、一度に 1 つのメニューだけに格納できます。また、同じメニューに複数回追加することはできません。複数のメニューで MenuItem を再利用するには、 MenuItem クラスの CloneMenu メソッドを使用します。以前追加した MenuItem を削除するには、 Remove メソッドを使用します。
このバージョンの Add メソッドを使用すると、以前作成した MenuItem オブジェクトをメニュー項目コレクションの末尾に追加できます。
使用例
[Visual Basic, C#, C++] 派生クラス MainMenu のインスタンスを作成し、 MenuItem オブジェクトのコレクションに MenuItem を追加する例を次に示します。この例で定義されるメソッドは、フォームのクラス内に記述し、そのフォーム クラスのメソッドから呼び出されることを前提にしています。
Private Sub InitializeMyMainMenu()
' Create the MainMenu and the MenuItem to add.
Dim mainMenu1 As New MainMenu()
Dim menuItem1 As New MenuItem("&File")
' Use the MenuItems property to call the Add method
' to add the MenuItem to the MainMenu menu item collection.
mainMenu1.MenuItems.Add(menuItem1)
' Assign mainMenu1 to the form.
Me.Menu = mainMenu1
End Sub
[C#]
private void InitializeMyMainMenu()
{
// Create the MainMenu and the MenuItem to add.
MainMenu mainMenu1 = new MainMenu();
MenuItem menuItem1 = new MenuItem("&File");
/* Use the MenuItems property to call the Add method
to add the MenuItem to the MainMenu menu item collection. */
mainMenu1.MenuItems.Add (menuItem1);
// Assign mainMenu1 to the form.
this.Menu = mainMenu1;
}
[C++]
private:
void InitializeMyMainMenu()
{
// Create the MainMenu and the MenuItem to add.
MainMenu* mainMenu1 = new MainMenu();
MenuItem* menuItem1 = new MenuItem(S"&File");
/* Use the MenuItems property to call the Add method
to add the MenuItem to the MainMenu menu item collection. */
mainMenu1->MenuItems->Add (menuItem1);
// Assign mainMenu1 to the form.
this->Menu = mainMenu1;
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
参照
Menu.MenuItemCollection クラス | Menu.MenuItemCollection メンバ | System.Windows.Forms 名前空間 | Menu.MenuItemCollection.Add オーバーロードの一覧 | Remove