TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# MenuStrip: Windows Forms

This C# tutorial demonstrates the MenuStrip control in Windows Forms.

MenuStrip adds a menu bar to your 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. We demonstrate the MenuStrip and provide some usage tips.

Start. To begin, please open the Toolbox window in Visual Studio. This should be in a Windows Forms project. Then, locate the MenuStrip entry and double-click it or drag it to the window of your form.

Insert standard items. This is an optional step but one that may help 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.

Note: None of these entries will actually have any effect if you click on them. This will be fixed in the next step.

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, you can add your custom C# code to that method's body.

Here: I simply call MessageBox.Show when the New or Open items are used. This displays a nice dialog box.

MessageBox.Show Examples

C# program that implements Click event handlers

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");
	}
    }
}

Result
    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. Simply select the menu in Visual Studio and press the forward-delete key or right-click and select Delete. This is useful for removing the Help menu in your application.

Move items. The designer in Visual Studio supports dragging elements around in menus to rearrange them. This is useful for reordering the commands. You can also reorder separators (which can be created with Convert To—see below).

To add new items to the menu, locate the box that says Type Here in gray letters and click there. 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).

Convert to. You can use Convert To by right-clicking on an item in your menu. Your options are MenuItem, ComboBox, Separator and TextBox. The default item is MenuItem, which indicates a plain text entry.

Also: The Separator is a simple horizontal line. The TextBox allows the user to enter text inline in a menu.

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 (&). The Exit command is encoded as E&xit. This means the 'x' is the access key.

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. Many aspects come into play.

Tip: There are icon sets available (from Microsoft and other organizations) that you can use to add free icons to your program.

Dock. It is usually a lot easier to dock your MenuStrip to one side of your application's window. Let us look at a MenuStrip that was docked to the bottom of a form. This may be confusing to a user.

And: Depending on whether you like this person or not, this could be a good or bad thing.

 

Summary. We explored many but not all aspects of the MenuStrip in the Windows Forms framework. As a container control for ToolStripMenuItems, the MenuStrip can make your life much easier if you ever need a menu bar.

Also: Visual Studio gives you the option to instantly add the default items. You can then use the Click event handler.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf