You can provide a standard menu for your forms with the MenuStrip control.
This walkthrough demonstrates how to use a MenuStrip control to create a standard menu. The form also responds when a user selects a menu item. The following tasks are illustrated in this walkthrough:
When you are finished, you will have a form with a standard menu that displays menu item selections in a StatusStrip control.
To copy the code in this topic as a single listing, see How to: Provide Standard Menu Items to a Form.
You'll need Visual Studio to complete this walkthrough.
The Windows Forms Designer can automatically populate a MenuStrip control with standard menu items.
Use the StatusStrip control to display status for your Windows Forms applications. In the current example, menu items selected by the user are displayed in a StatusStrip control.
Handle the DropDownItemClicked event to respond when the user selects a menu item.
// This method is the DropDownItemClicked event handler. // It passes the ClickedItem object to a utility method // called UpdateStatus, which updates the text displayed // in the StatusStrip control. private void fileToolStripMenuItem_DropDownItemClicked( object sender, ToolStripItemClickedEventArgs e)
' This method is the DropDownItemClicked event handler. ' It passes the ClickedItem object to a utility method ' called UpdateStatus, which updates the text displayed ' in the StatusStrip control. Private Sub FileToolStripMenuItem_DropDownItemClicked( _ ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) _ Handles FileToolStripMenuItem.DropDownItemClicked Me.UpdateStatus(e.ClickedItem) End Sub
// This utility method assigns the value of a ToolStripItem // control's Text property to the Text property of the // ToolStripStatusLabel. private void UpdateStatus(ToolStripItem item) < if (item != null) < string msg = String.Format("selected", item.Text); this.statusStrip1.Items[0].Text = msg; > >
' This utility method assigns the value of a ToolStripItem ' control's Text property to the Text property of the ' ToolStripStatusLabel. Private Sub UpdateStatus(ByVal item As ToolStripItem) If item IsNot Nothing Then Dim msg As String = String.Format(" selected", item.Text) Me.StatusStrip1.Items(0).Text = msg End If End Sub
In this walkthrough, you have created a form with a standard menu. You can use the ToolStrip family of controls for many other purposes:
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.