MenuStrip
This adds a menu bar to a Windows Forms program. With this control, we add a menu area and then add the default menus or create custom menus directly in Visual Studio.
To begin, please open the Toolbox window in Visual Studio. Then, locate the MenuStrip
entry and double-click it or drag it to the window of your form.
To add actions to the items in your MenuStrip
, double click them and then a Click event handler for that ToolStripMenuItem
will be inserted.
MessageBox.Show
when the New or Open items are used.using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void newToolStripMenuItem_Click(object sender, EventArgs e) { MessageBox.Show("New command executed"); } private void openToolStripMenuItem_Click(object sender, EventArgs e) { MessageBox.Show("Open command executed"); } } }Message boxes will appear when New/Open are clicked. Message boxes will appear when Ctrl-N/Ctrl-O are pressed.
You can easily remove a menu from your MenuStrip
. Select the menu in Visual Studio and press the forward-delete key or right-click and select Delete
.
How are access keys implemented in ToolStripMenuItems
? To add an access key, precede the letter you want to become the key with an ampersand.
We explored many aspects of the MenuStrip
. As a container control for ToolStripMenuItems
, the MenuStrip
can make your life much easier if you ever need a menu bar.