DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Using Menus on Windows Forms in VS .NET 2008 and VB

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 VB.

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 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!

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 Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub

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 Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
MessageBox.Show("Created with Visual Studio .NET 2008 for dotNetTutorials.com, 2008")
End Sub

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.

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!

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 Sub PerformCalculationToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PerformCalculationToolStripMenuItem.Click
Label5.Text = TextBox1.Text & " + " & TextBox2.Text & " = " & (Convert.ToInt16(TextBox1.Text) + Convert.ToInt16(TextBox2.Text))
End Sub

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 Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text <> "" AndAlso TextBox2.Text <> "" Then
ContextMenuStrip1.Items("PerformCalculationToolStripMenuItem").Enabled = True
Else
ContextMenuStrip1.Items("PerformCalculationToolStripMenuItem").Enabled = False
End If
End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
If TextBox1.Text <> "" AndAlso TextBox2.Text <> "" Then
ContextMenuStrip1.Items("PerformCalculationToolStripMenuItem").Enabled = True
Else
ContextMenuStrip1.Items("PerformCalculationToolStripMenuItem").Enabled = False
End If
End Sub

The entire code-behind will look something like this:

Public Class Form1
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text <> "" AndAlso TextBox2.Text <> "" Then
ContextMenuStrip1.Items("PerformCalculationToolStripMenuItem").Enabled = True
Else
ContextMenuStrip1.Items("PerformCalculationToolStripMenuItem").Enabled = False
End If
End Sub

Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
MessageBox.Show("Created with Visual Studio .NET 2008 for dotNetTutorials.com, 2008")
End Sub

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
If TextBox1.Text <> "" AndAlso TextBox2.Text <> "" Then
ContextMenuStrip1.Items("PerformCalculationToolStripMenuItem").Enabled = True
Else
ContextMenuStrip1.Items("PerformCalculationToolStripMenuItem").Enabled = False
End If
End Sub

Private Sub PerformCalculationToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PerformCalculationToolStripMenuItem.Click
Label5.Text = TextBox1.Text & " + " & TextBox2.Text & " = " & (Convert.ToInt16(TextBox1.Text) + Convert.ToInt16(TextBox2.Text))
End Sub
End Class

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

Looking for the C#.NET 2008 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!