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.
Click handlers. To add actions to the items in your MenuStrip, double click them and then a Click event handler for that ToolStripMenuItem will be inserted.
Then We can add custom C# code to that method's body. We call 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.
Insert standard items. This may speed up the development of many programs. Select the "Insert Standard Items" link in the popup that appears in Visual Studio.
And This will add the File, Edit, Tools and Help menus with standard sub-menu items.
Change menus. 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.
Move items. The designer in Visual Studio supports dragging elements around in menus to rearrange them. This is useful for reordering the commands.
And To add new items to the menu, locate the box that says Type Here in gray letters and click there.
Then Type the text of the new menu item you want to add. Next, you can drag the item (to move it) or convert it (to change its type).
Access keys. How are access keys implemented in ToolStripMenuItems? To add an access key, precede the letter you want to become the key with an ampersand.
Tip To use access keys, please press Alt and then the letters directing to your command. So to use Open, press Alt-F-O.
Set image. ToolStripMenuItems in a MenuStrip can have images associated with them. Unfortunately, achieving a good visual effect in your application can involve a lot of trial and error.
Summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 28, 2022 (edit).