다음을 통해 공유


방법: Windows Forms ContextMenu 구성 요소를 사용하여 메뉴 항목 추가 및 제거

Windows Forms에서 바로 가기 메뉴 항목을 추가하고 제거하는 방법을 설명합니다.

Windows Forms ContextMenu 구성 요소는 선택한 개체와 관련된 자주 사용되는 명령 메뉴를 제공합니다. MenuItem 개체를 MenuItems 컬렉션에 추가하여 바로 가기 메뉴에 항목을 추가할 수 있습니다.

바로 가기 메뉴에서 항목을 영구적으로 제거할 수 있습니다. 그러나 런타임에 항목을 숨기거나 사용하지 않도록 설정하는 것이 더 적합할 수 있습니다.

중요합니다

MenuStripContextMenuStripMainMenuContextMenu 컨트롤에 새로운 기능이 추가된 것으로, 이전 컨트롤 버전을 대체합니다. 그러나 이전 버전과의 호환성 및 앞으로의 사용 가능성을 고려하여 MainMenuContextMenu를 유지하도록 선택할 수 있습니다.

바로 가기 메뉴에서 항목을 제거하려면

  1. ContextMenu 구성 요소 MenuItems 컬렉션의 Remove 또는 RemoveAt 메서드를 사용하여 특정 메뉴 항목을 제거합니다.

    ' Removes the first item in the shortcut menu.
    ContextMenu1.MenuItems.RemoveAt(0)
    ' Removes a particular object from the shortcut menu.
    ContextMenu1.MenuItems.Remove(mnuItemNew)
    
    // Removes the first item in the shortcut menu.
    contextMenu1.MenuItems.RemoveAt(0);
    // Removes a particular object from the shortcut menu.
    contextMenu1.MenuItems.Remove(mnuItemNew);
    
    // Removes the first item in the shortcut menu.
    contextMenu1->MenuItems->RemoveAt(0);
    // Removes a particular object from the shortcut menu.
    contextMenu1->MenuItems->Remove(mnuItemNew);
    

    -또는-

  2. ContextMenu 구성 요소 MenuItems 컬렉션의 Clear 메서드를 사용하여 메뉴에서 모든 항목을 제거합니다.

    ContextMenu1.MenuItems.Clear()
    
    contextMenu1.MenuItems.Clear();
    
    contextMenu1->MenuItems->Clear();
    

참고하십시오