MainMenu - ahatornn/clforms GitHub Wiki

MainMenu

ClForms.Elements.Menu.MainMenu
Represents the menu structure of a window

public class MainMenu: Control, IElementStyle<MainMenu>

Inheritance Control -> MainMenu
Derived IElementStyle<MainMenu>

Examples

The following code example instantiates and creates a MainMenu and some MenuItems with specified Text property and OnClick event

var mainMenu1 = new MainMenu();

var paramItem = new MenuItem("Window");

var maxParamItem = new MenuItem("Maximized");
maxParamItem.Tag = ControlState.Maximized;
maxParamItem.OnClick += WindowStateChangedClick;
paramItem.Items.Add(maxParamItem);

paramItem.Items.Add(new SeparatorMenuItem());

var exitItem = new MenuItem("Exit");
exitItem.OnClick += ExitClick;
paramItem.Items.Add(exitItem);
mainMenu1.Items.Add(paramItem);

this.MainMenu = mainMenu1;private void WindowStateChangedClick(object sender, EventArgs e)
{
    if (sender is MenuItem targetMenuItem)
    {
        targetWindow.WindowState = (ControlState) targetMenuItem.Tag;
    }
}private void ExitClick(object sender, EventArgs e) => this.Close();

You can find more examples of using the CheckBoxGroup in this project

Remarks

The MainMenu control represents the container for the menu structure of a form. A menu is composed of MenuItem objects that represent the individual menu commands in the menu structure. Each MenuItem can be a command for your application or a parent menu for other submenu items. To bind the MainMenu to the Window that will display it, assign the MainMenu to the MainMenu property of the Window.
The MenuItem class provides properties that enable you to configure the appearance and functionality of a menu item. To display a check mark next to a menu item, use the Checked property. You can use this feature to identify a menu item that is selected in a list of mutually exclusive menu items. For example, if you have a set of menu items for setting the color of text in a TextBox control, you can use the Checked property to identify which color is currently selected. The Shortcut property can be used to define a keyboard combination that can be pressed to select the menu item.

Constructors

Syntax Description
MainMenu() Initialize a new instance MainMenu

Properties

Name Type Description
AutoSize bool Gets or sets a value indicating whether the control is resized in accordance with its contents
Background Color Gets or sets a brush that describes the background of a control
BackgroundIsTransparent bool Gets a value indicating whether the Background has Color.NotSet
Bounds Rect Gets the size and location of the control including its nonclient elements, in points, relative to the parent control
DesiredSize Size Gets the size that this element computed during the measure pass of the layout process
DisabledBackground Color Gets or sets a value to display of background when the control is disabled
DisabledForeground Color Gets or sets a value to display of foreground when the control is disabled
DrawingContext IDrawingContext Gets a value of the drawing context
Foreground Color Gets or sets a brush that describes the text of a control
ForegroundIsTransparent bool Gets a value indicating whether the Foreground has Color.NotSet
Height int? Gets or sets the height of the control
Id long Gets a value of the control's identifier
IsMeasureValid bool Gets a value indicating whether component sizing was performed
IsOpened bool Gets a value indicating that menu is opened
IsVisualValid bool Gets a value indicating whether the component is being re-rendered
Items MenuItemCollection Gets a value indicating the collection of MenuItemBase objects associated with the menu
Location Point Gets or sets the coordinates of the upper-left corner of the control relative to the upper-left corner of its container
Margin Thickness Gets or sets the outer margin of an element
MnemonicForeground Color Gets or sets a value indicating the color of the assigned character associated with accessing the menu item
Padding Thickness Gets or sets a Thickness value that describes the amount of space between a control and its child element
Parent ContentControl Gets or sets the parent container of the control
SelectedBackground Color Gets or sets a value to display of background when the item is selected
SelectedForeground Color Gets or sets a value to display of foreground when the item is selected
Tag object Gets or sets the object that contains data about the control
Width int? Gets or sets the width of the control

Methods

Syntax Description
Arrange(Rect) Positions child elements and determines a size for a Control. Parent elements call this method from their Arrange(Rect) implementation to form a recursive layout update
Close() Manually closes a MainMenu
InvalidateMeasure() Invalidates the measurement state (layout) for the element
InvalidateMeasureIfAutoSize() Invalidates the measurement state (layout) for the element if AutoSize property is true otherwise invalidates the rendering of the element
InvalidateVisual() Invalidates the rendering of the element, and forces a complete new layout pass.
FindAndClick(ConsoleKeyInfo keyInfo) Trying to find an item associated with the specified ConsoleKeyInfo
Measure(Size) Updates DesiredSize of a Control. Parent elements call this method from their own Measure(Size) implementations to form a recursive layout update
OnRender(IDrawingContext) Filling a pseudographics drawing context
Open() Manually open a MainMenu
ParentWindow() Gets the form the control is in
SetStyle(Action) Defines actions with an element style

Events

Event Description
OnAutoSizeChanged Occurs when the value of the AutoSize property changes
OnBackgroundChanged Occurs when the value of the Background property changes
OnClose Occurs when the main menu collapses
OnDisabledBackgroundChanged Occurs when the value of the DisabledBackground property changes
OnDisabledForegroundChanged Occurs when the value of the DisabledForeground property changes
OnForegroundChanged Occurs when the value of the Foreground property changes
OnHeightChanged Occurs when the value of the Height property changes
OnMarginChanged Occurs when the value of the Margin property changes
OnMnemonicForegroundChanged Occurs when the value of the MnemonicForeground property changes
OnOpen Occurs when the main menu open
OnPaddingChanged Occurs when the value of the Padding property changes
OnParentChanged Occurs when the value of the Parent property changes
OnPopup Occurs before the main menu open sub menu
OnSelectedBackgroundChanged Occurs when the value of the SelectedBackground property changes
OnSelectedForegroundChanged Occurs when the value of the SelectedForeground property changes
OnTagChanged Occurs when the value of the Tag property changes
OnWidthChanged Occurs when the value of the Width property changes
⚠️ **GitHub.com Fallback** ⚠️