Menu.MenuItemCollection.Add Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Dodaje nowy MenuItem element do kolekcji.
Przeciążenia
| Add(String) |
Dodaje nowy MenuItemelement , na końcu bieżącego menu z określonym podpisem. |
| Add(MenuItem) |
Dodaje wcześniej utworzony MenuItem element na końcu bieżącego menu. |
| Add(Int32, MenuItem) |
Dodaje wcześniej utworzony MenuItem w określonym indeksie w kolekcji elementów menu. |
| Add(String, EventHandler) |
Dodaje nowy MenuItem na końcu bieżącego menu z określonym podpisem i określoną procedurą obsługi zdarzeń Click dla zdarzenia. |
| Add(String, MenuItem[]) |
Dodaje nowy MenuItem element na końcu tego menu z określonym podpisem, Click procedurą obsługi zdarzeń i elementami. |
Add(String)
Dodaje nowy MenuItemelement , na końcu bieżącego menu z określonym podpisem.
public:
virtual System::Windows::Forms::MenuItem ^ Add(System::String ^ caption);
public virtual System.Windows.Forms.MenuItem Add (string caption);
abstract member Add : string -> System.Windows.Forms.MenuItem
override this.Add : string -> System.Windows.Forms.MenuItem
Public Overridable Function Add (caption As String) As MenuItem
Parametry
- caption
- String
Podpis elementu menu.
Zwraca
Element MenuItem , który reprezentuje element menu dodawany do kolekcji.
Przykłady
Poniższy przykład kodu używa klasy MainMenu pochodnej do utworzenia menu głównego , mainMenu1który zawiera dwa MenuItem obiekty dodane do swojej MenuItems kolekcji. Następnie kod przypisuje mainMenu1 się do Menu właściwości obiektu Form. Ten przykład wymaga, aby kod zdefiniowany w tym przykładzie znajdował się w formularzu.
private:
void InitializeMyMainMenu()
{
// Create the MainMenu.
MainMenu^ mainMenu1 = gcnew MainMenu;
/* Use the MenuItems property to call the Add method
to add two new MenuItem objects to the MainMenu. */
mainMenu1->MenuItems->Add( "&File" );
mainMenu1->MenuItems->Add( "&Edit" );
// Assign mainMenu1 to the form.
this->Menu = mainMenu1;
}
private void InitializeMyMainMenu()
{
// Create the MainMenu.
MainMenu mainMenu1 = new MainMenu();
/* Use the MenuItems property to call the Add method
to add two new MenuItem objects to the MainMenu. */
mainMenu1.MenuItems.Add ("&File");
mainMenu1.MenuItems.Add ("&Edit");
// Assign mainMenu1 to the form.
this.Menu = mainMenu1;
}
Private Sub InitializeMyMainMenu()
' Create the MainMenu.
Dim mainMenu1 As New MainMenu()
' Use the MenuItems property to call the Add method
' to add two new MenuItem objects to the MainMenu.
mainMenu1.MenuItems.Add("&File")
mainMenu1.MenuItems.Add("&Edit")
' Assign mainMenu1 to the form.
Me.Menu = mainMenu1
End Sub
Uwagi
Element MenuItem może być zawarty tylko w jednym menu jednocześnie i nie można go dodać więcej niż raz do tego samego menu. Aby ponownie użyć MenuItem elementu w więcej niż jednym menu, użyj CloneMenu metody MenuItem klasy . Aby usunąć MenuItem element, który został wcześniej dodany, użyj Remove metody .
Zobacz też
Dotyczy
Add(MenuItem)
Dodaje wcześniej utworzony MenuItem element na końcu bieżącego menu.
public:
virtual int Add(System::Windows::Forms::MenuItem ^ item);
public virtual int Add (System.Windows.Forms.MenuItem item);
abstract member Add : System.Windows.Forms.MenuItem -> int
override this.Add : System.Windows.Forms.MenuItem -> int
Public Overridable Function Add (item As MenuItem) As Integer
Parametry
Zwraca
Indeks oparty na zera, w którym element jest przechowywany w kolekcji.
Przykłady
Poniższy przykład kodu tworzy wystąpienie klasy MainMenupochodnej , i dodaje obiekt MenuItem do kolekcji MenuItem obiektów. Ten przykład wymaga, aby metoda zdefiniowana w tym przykładzie znajdowała się w klasie formularza i wywoływana przez metodę w tej klasie formularzy.
private:
void InitializeMyMainMenu()
{
// Create the MainMenu and the MenuItem to add.
MainMenu^ mainMenu1 = gcnew MainMenu;
MenuItem^ menuItem1 = gcnew 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;
}
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;
}
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
Uwagi
Element MenuItem może być zawarty tylko w jednym menu jednocześnie i nie można go dodać więcej niż raz do tego samego menu. Aby ponownie użyć MenuItem elementu w więcej niż jednym menu, użyj CloneMenu metody MenuItem klasy . Aby usunąć MenuItem element, który został wcześniej dodany, użyj Remove metody .
Ta wersja Add metody umożliwia dodawanie wcześniej utworzonych MenuItem obiektów na końcu kolekcji elementów menu.
Zobacz też
Dotyczy
Add(Int32, MenuItem)
Dodaje wcześniej utworzony MenuItem w określonym indeksie w kolekcji elementów menu.
public:
virtual int Add(int index, System::Windows::Forms::MenuItem ^ item);
public virtual int Add (int index, System.Windows.Forms.MenuItem item);
abstract member Add : int * System.Windows.Forms.MenuItem -> int
override this.Add : int * System.Windows.Forms.MenuItem -> int
Public Overridable Function Add (index As Integer, item As MenuItem) As Integer
Parametry
- index
- Int32
Pozycja dodawania nowego elementu.
Zwraca
Indeks oparty na zera, w którym element jest przechowywany w kolekcji.
Wyjątki
Indeks podany w parametrze index jest większy niż rozmiar kolekcji.
Przykłady
Poniższy przykład kodu tworzy wystąpienie klasy MainMenupochodnej , i dodaje MenuItem obiekt do kolekcji obiektów w określonej lokalizacji w kolekcji MenuItem elementów menu. Ten przykład wymaga, aby metoda zdefiniowana w tym przykładzie znajdowała się w klasie formularza i wywoływana przez metodę w tej klasie formularzy.
private:
void InitializeMyMainMenu()
{
// Create the MainMenu and the MenuItem to add.
MainMenu^ mainMenu1 = gcnew MainMenu;
MenuItem^ menuItem1 = gcnew MenuItem( "&File" );
/* Use the MenuItems property to call the Add method
to add the MenuItem to mainMenu1 at specific index. */
mainMenu1->MenuItems->Add( 0, menuItem1 );
// Assign mainMenu1 to the form.
this->Menu = mainMenu1;
}
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 mainMenu1 at specific index. */
mainMenu1.MenuItems.Add (0, menuItem1);
// Assign mainMenu1 to the form.
this.Menu = mainMenu1;
}
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 mainMenu1 at specific index.
mainMenu1.MenuItems.Add(0, menuItem1)
' Assign mainMenu1 to the form.
Me.Menu = mainMenu1
End Sub
Uwagi
Element MenuItem może być zawarty tylko w jednym menu jednocześnie i nie można go dodać więcej niż raz do tego samego menu. Aby ponownie użyć MenuItem elementu w więcej niż jednym menu, użyj CloneMenu metody MenuItem klasy . Aby usunąć MenuItem element, który został wcześniej dodany, użyj Remove metody .
Ta wersja Add metody umożliwia dodawanie wcześniej utworzonych MenuItem obiektów do określonej lokalizacji indeksu w kolekcji. Wszystkie MenuItem obecnie znajdujące się w tym indeksie i wszystkie MenuItem obiekty po tym indeksie są przenoszone do następnego najniższego indeksu w kolekcji.
Zobacz też
Dotyczy
Add(String, EventHandler)
public:
virtual System::Windows::Forms::MenuItem ^ Add(System::String ^ caption, EventHandler ^ onClick);
public virtual System.Windows.Forms.MenuItem Add (string caption, EventHandler onClick);
abstract member Add : string * EventHandler -> System.Windows.Forms.MenuItem
override this.Add : string * EventHandler -> System.Windows.Forms.MenuItem
Public Overridable Function Add (caption As String, onClick As EventHandler) As MenuItem
Parametry
- caption
- String
Podpis elementu menu.
- onClick
- EventHandler
Element EventHandler reprezentujący procedurę obsługi zdarzeń wywoływaną po kliknięciu elementu przez użytkownika lub naciśnięciu przez użytkownika akceleratora lub klawisza skrótu dla elementu menu.
Zwraca
Element MenuItem , który reprezentuje element menu dodawany do kolekcji.
Przykłady
Poniższy przykład kodu używa klasy MainMenu pochodnej do utworzenia menu głównego , mainMenu1który zawiera dwa MenuItem obiekty dodane do swojej MenuItems kolekcji. Kod używa tej wersji Add metody, aby zdefiniować procedurę obsługi zdarzeń dla Click zdarzenia drugiego elementu menu dodanego do kolekcji. Następnie kod przypisuje mainMenu1 się do Menu właściwości obiektu Form. Ten przykład wymaga, aby kod zdefiniowany w tym przykładzie znajdował się w formularzu.
private:
void InitializeMyMainMenu()
{
// Create the MainMenu.
MainMenu^ mainMenu1 = gcnew MainMenu;
/* Use the MenuItems property to call the Add method
to add two new MenuItem objects to the MainMenu. */
mainMenu1->MenuItems->Add( "&File" );
mainMenu1->MenuItems->Add( "&Edit", gcnew EventHandler(
this, &Form1::menuItem2_Click ) );
// Assign mainMenu1 to the form.
this->Menu = mainMenu1;
}
private:
void menuItem2_Click( System::Object^ sender, System::EventArgs^ e )
{
// Insert code to handle Click event.
}
private void InitializeMyMainMenu()
{
// Create the MainMenu.
MainMenu mainMenu1 = new MainMenu();
/* Use the MenuItems property to call the Add method
to add two new MenuItem objects to the MainMenu. */
mainMenu1.MenuItems.Add ("&File");
mainMenu1.MenuItems.Add ("&Edit", new EventHandler (menuItem2_Click));
// Assign mainMenu1 to the form.
this.Menu = mainMenu1;
}
private void menuItem2_Click(System.Object sender, System.EventArgs e)
{
// Insert code to handle Click event.
}
Private Sub InitializeMyMainMenu()
' Create the MainMenu.
Dim mainMenu1 As New MainMenu()
' Use the MenuItems property to call the Add method
' to add two new MenuItem objects to the MainMenu.
mainMenu1.MenuItems.Add("&File")
mainMenu1.MenuItems.Add("&Edit", _
New EventHandler(AddressOf menuItem2_Click))
' Assign mainMenu1 to the form.
Me.Menu = mainMenu1
End Sub
Private Sub menuItem2_Click(sender As System.Object, e As System.EventArgs)
' Insert code to handle Click event.
End Sub
Uwagi
Element MenuItem może być zawarty tylko w jednym menu jednocześnie i nie można go dodać więcej niż raz do tego samego menu. Aby ponownie użyć MenuItem elementu w więcej niż jednym menu, użyj CloneMenu metody MenuItem klasy . Aby usunąć MenuItem element, który został wcześniej dodany, użyj Remove metody .
Ta wersja Add metody umożliwia określenie podpisu dla elementu menu i delegata do obsługi Click zdarzenia. Możesz użyć tej wersji Add metody, jeśli aplikacja ma już program obsługi zdarzeń do obsługi zdarzenia Click .
Zobacz też
Dotyczy
Add(String, MenuItem[])
public:
virtual System::Windows::Forms::MenuItem ^ Add(System::String ^ caption, cli::array <System::Windows::Forms::MenuItem ^> ^ items);
public virtual System.Windows.Forms.MenuItem Add (string caption, System.Windows.Forms.MenuItem[] items);
abstract member Add : string * System.Windows.Forms.MenuItem[] -> System.Windows.Forms.MenuItem
override this.Add : string * System.Windows.Forms.MenuItem[] -> System.Windows.Forms.MenuItem
Public Overridable Function Add (caption As String, items As MenuItem()) As MenuItem
Parametry
- caption
- String
Podpis elementu menu.
Zwraca
Element MenuItem , który reprezentuje element menu dodawany do kolekcji.
Uwagi
Element MenuItem może być zawarty tylko w jednym menu jednocześnie i nie można go dodać więcej niż raz do tego samego menu. Aby ponownie użyć MenuItem elementu w więcej niż jednym menu, użyj CloneMenu metody MenuItem klasy . Aby usunąć MenuItem element, który został wcześniej dodany, użyj Remove metody .
Ta wersja Add metody umożliwia określenie podpisu dla elementu menu i delegata, który będzie obsługiwać jego Click zdarzenie. Możesz użyć tej wersji Add metody, jeśli aplikacja ma już program obsługi zdarzeń do obsługi zdarzenia Click . Ta wersja metody umożliwia również określenie tablicy wcześniej utworzonych MenuItem obiektów, które mają zostać dodane do kolekcji. Tej funkcji można użyć do ponownego użycia istniejących MenuItem obiektów, które zostały sklonowane przy użyciu CloneMenu metody . items Jeśli parametr nie jest pusty lub null, MenuItem dodawany do kolekcji będzie zawierać elementy podrzędne.