DotNet Tutorials

Server Intellect

 Using Menus on Windows Forms in VS .NET 2008 and C#

This tutorial will show how to add a Main Menu Strip and Context Menus to your Windows Forms, as well as how to add code to them to make them functional. This was created with Visual Studio .NET 2008 and C#.

This tutorial and exmaple was written in Visual Studio .NET 2008. Although the same principles apply in 2005, the steps taken may be slightly different.

We are going to explore using Menu Controls in this tutorial for Windows Forms. We will see how we can add a Main Menu Strip at the top of our form (File, Help, etc.) We will also see how we can add Context Menus (Right-click menus) and how we can add code to both these items to make them more functional. We will start by creating a New Project in VS, and choosing Windows Forms, C#.
This is what our finished form will look like:

We are going to create two TextBoxes and allow the user to right-click on them where there will be an option to Perform Calculation. However, this option will only be available if the two TextBoxes are not empty. We can start by adding the Menu Strip - Drag onto the form a MenuStrip Control from the Menus & Toolbars toolbox, or simply double-click it. Once in place, we will be able to edit it. You will see a Type Here box, which will allow us to do just that if we click on it to highlight it. Go ahead and add a File and a Help menu item, then under File, add Exit, and under Help, add About.

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

Now if you run this program, we will be able to see the MenuStrip, but it doesn't do anything yet. It has no functionality; it just looks good. To add logic to it, we can click on it once (to select; not highlight) and then either goto the Events window in the Properties and double-click on the Click event, or we can simply double-click on the MenuItem and it will create the handler for us and take us to the code-behind.

First we will add the functionality to the Exit menu item, so double-click on the Exit menu item to generate an Event handler for the Click. Once we are at the code-behind, we can add the following code:

private void existToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}

This simply terminates the application. Now do the same to the About sub-menu item. For this one, we will create a Message Box to tell the user more about the program:

private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Created with Visual Studio .NET 2008 for dotNetTutorials.com, 2008");
}

Now when our sub-menu items are clicked, we will have some functionality. The About menu item will display a Message Box to the user with information about the program.
Next, go ahead and add a couple textboxes. Position them where you want, and then add a ContextMenuStrip Control from the toolbox. The positioning of this does not matter as it will be hidden and appear whereever the user right-clicks. Once we have added the Context Menu, we need to specify which Controls will use it. So go to the Properties of each of the text boxes and modify the ContextMenuStrip attribute of both text boxes to the one we just created. Now when the user right-clicks on either of those text boxes, they will get the Context Menu we just created. Although it doesn't have any menu items as yet.

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

To select the Context Menu Strip again, click on it in the gray bar at the bottom of our design view. It should then show up on your form. Now we can add Menu Items the same way as we did with the MenuStrip earlier. Let's add just the one Menu Item - Perform Calculation, and set its Enabled attribute to false. We will enable it again when the text boxes are filled.

Next, add a label to the form. This label will display the result of the calculation. Then create a handler for the menu item's click event the same way we did the last one. Now we are going to perform the calculation with this:

private void mnuPerformCalculation_Click(object sender, EventArgs e)
{
label5.Text = textBox1.Text + " + " + textBox2.Text+ " = " + (Convert.ToInt16(textBox1.Text) + Convert.ToInt16(textBox2.Text));
}

This program is now fully functional. But we can add some validation to it. It would be wise to disable the Perform Calculation menu item until both text boxes have data (been typed into). We can do this by adding the following code to the text box TextChanged or KeyUp events:

private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if(textBox1.Text != "" && textBox2.Text != "")
{
contextMenuStrip1.Items["mnuPerformCalculation"].Enabled = true;
}
else
{
contextMenuStrip1.Items["mnuPerformCalculation"].Enabled = false;
}
}

private void textBox2_TextChanged(object sender, EventArgs e)
{
if(textBox1.Text != "" && textBox2.Text != "")
{
contextMenuStrip1.Items["mnuPerformCalculation"].Enabled = true;
}
else
{
contextMenuStrip1.Items["mnuPerformCalculation"].Enabled = false;
}
}

The entire code-behind will look something like this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinFormMenus_cs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void existToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}

private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Created with Visual Studio .NET 2008 for dotNetTutorials.com, 2008");
}

private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if(textBox1.Text != "" && textBox2.Text != "")
{
contextMenuStrip1.Items["mnuPerformCalculation"].Enabled = true;
}
else
{
contextMenuStrip1.Items["mnuPerformCalculation"].Enabled = false;
}
}

private void textBox2_TextChanged(object sender, EventArgs e)
{
if(textBox1.Text != "" && textBox2.Text != "")
{
contextMenuStrip1.Items["mnuPerformCalculation"].Enabled = true;
}
else
{
contextMenuStrip1.Items["mnuPerformCalculation"].Enabled = false;
}
}

private void mnuPerformCalculation_Click(object sender, EventArgs e)
{
label5.Text = textBox1.Text + " + " + textBox2.Text+ " = " + (Convert.ToInt16(textBox1.Text) + Convert.ToInt16(textBox2.Text));
}
}
}

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

Looking for the 2008 VB Version? Click Here!

Looking for more .NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 
123 ASP

411 ASP

Dot Net Freaks

Server Intellect