다음을 통해 공유


MenuItem 클래스

MainMenu 또는 ContextMenu 내에 표시되는 개별 항목을 나타냅니다. ToolStripMenuItem에 의해 이전 버전의 MenuItem 컨트롤의 여러 기능이 교체 및 추가되었지만 MenuItem는 이전 버전과의 호환성을 위해 유지되었으며, 필요한 경우 선택해서 사용할 수 있습니다.

네임스페이스: System.Windows.Forms
어셈블리: System.Windows.Forms(system.windows.forms.dll)

구문

‘선언
Public Class MenuItem
    Inherits Menu
‘사용 방법
Dim instance As MenuItem
public class MenuItem : Menu
public ref class MenuItem : public Menu
public class MenuItem extends Menu
public class MenuItem extends Menu

설명

MenuItem이 표시되도록 하려면 해당 항목을 MainMenu 또는 ContextMenu에 추가해야 합니다. 하위 메뉴를 만들려면 부모 MenuItemMenuItems 속성에 MenuItem 개체를 추가합니다.

MenuItem 클래스에는 메뉴 항목의 모양 및 기능을 구성할 수 있는 속성이 들어 있습니다. 메뉴 항목 옆에 확인 표시를 하려면 Checked 속성을 사용합니다. 이 기능을 사용하면 함께 사용할 수 없는 메뉴 항목 목록에서 선택된 메뉴 항목을 식별할 수 있습니다. 예를 들어, TextBox 컨트롤에 텍스트 색 설정을 위한 메뉴 항목 집합이 있는 경우 Checked 속성을 사용하여 현재 선택된 색을 식별할 수 있습니다. Shortcut 속성을 사용하면 메뉴 항목을 눌러 선택하는 키보드 조합을 정의할 수 있습니다.

MDI(다중 문서 인터페이스) 응용 프로그램에 표시되는 MenuItem 개체의 경우 MergeMenu 메서드를 사용하면 MDI 부모의 메뉴와 자식 폼의 메뉴를 병합하여 통합 메뉴 구조를 만들 수 있습니다. MenuItemMainMenuContextMenu의 경우처럼 동시에 여러 위치에서 다시 사용할 수 없으므로, CloneMenu 메서드를 사용하여 다른 위치에서 사용할 MenuItem의 복사본을 만들 수 있습니다.

Popup 이벤트를 사용하면 메뉴가 표시되기 전에 작업을 수행할 수 있습니다. 예를 들어, 해당 이벤트의 이벤트 처리기를 만들어 코드 상태에 따라 메뉴 항목을 표시하거나 숨길 수 있습니다. 예를 들어, Select 이벤트를 사용하면 메뉴 항목 위로 마우스 포인터를 이동할 때 응용 프로그램의 메뉴 항목에 대한 자세한 도움말을 제공할 수 있습니다.

예제

다음 코드 예제에서는 폼의 메뉴 구조를 만듭니다. 이 예제 코드에서는 MenuItem을 추가하여 최상위 메뉴 항목을 나타내고, 이 메뉴 항목에 글꼴 크기를 선택하는 하위 메뉴 항목을 추가한 다음, 응용 프로그램에 큰 글꼴 및 작은 글꼴 선택 옵션을 나타내는 두 개의 하위 메뉴 항목을 그 메뉴 항목에 추가합니다. 이 예제에서는 mainMenu1이라는 MainMenu 개체가 있고 menuItem1, menuItem2, menuItem3menuItem4라는 네 개의 MenuItem 개체가 있어야 합니다.

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
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 void CreateMyMenu()
{
    // Set the caption for the top-level menu item.
    menuItem1.set_Text("Edit");

    // Set the caption for the first submenu.
    menuItem2.set_Text("Font Size");

    // Set the caption for menuItem2's first submenu.
    menuItem3.set_Text("Small");

    // Set the checked property to true since this is the default value.
    menuItem3.set_Checked(true);

    // Define a shortcut key combination for the menu item.
    menuItem3.set_Shortcut(Shortcut.CtrlS);

    // Set the caption of the second sub menu item of menuItem2.
    menuItem4.set_Text("Large");

    // Define a shortcut key combination for the menu item.
    menuItem4.set_Shortcut(Shortcut.CtrlL);

    // Set the index of the menu item so it is placed below
    //the first submenu item.
    menuItem4.set_Index(1);

    // Add menuItem3 and menuItem4 to menuItem2's list of menu items.
    menuItem2.get_MenuItems().Add(menuItem3);
    menuItem2.get_MenuItems().Add(menuItem4);

    // Add menuItem2 to menuItem1's list of menu items.
    menuItem1.get_MenuItems().Add(menuItem2);

    // Add menuItem1 to the MainMenu for displaying.
    mainMenu1.get_MenuItems().Add(menuItem1);
} //CreateMyMenu

상속 계층 구조

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.Menu
        System.Windows.Forms.MenuItem

스레드로부터의 안전성

이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

MenuItem 멤버
System.Windows.Forms 네임스페이스
MainMenu 클래스
Menu 클래스
MenuMerge
ContextMenu 클래스