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

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

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

바로 가기 메뉴에서 항목을 완전히 제거할 수도 있지만, 런타임에 항목을 숨기거나 사용할 수 없도록 표시하는 것이 더 나을 수도 있습니다.

중요

MenuStripContextMenuStrip 컨트롤은 MainMenuContextMenu 컨트롤에 새로운 기능이 추가된 것으로, 이전 컨트롤을 대체합니다. 그러나 이전 버전과의 호환성 및 앞으로의 사용 가능성을 고려하여 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.get_MenuItems().RemoveAt(0);
    // Removes a particular object from the shortcut menu.
    contextMenu1.get_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.get_MenuItems().Clear();
    
    contextMenu1->MenuItems->Clear();
    

참고 항목

참조

ContextMenu 구성 요소 개요(Windows Forms)

ContextMenu

기타 리소스

ContextMenu 구성 요소(Windows Forms)