This page was last reviewed on Dec 24, 2024.
Dot Net Perls
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.
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.
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.
Also The designer in Visual Studio supports dragging elements around in menus to rearrange them.
Tip 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.
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 Dec 24, 2024 (simplify).
Home
Changes
© 2007-2024 Sam Allen.