Menus
Up VB6 vs. VB.NET
Almost all serious computer programs have menus. Menus are an easy and familiar way for users to make choices regarding your program. Menus are typically used for opening or closing files, for cut and paste operations or for setting options for the program.

Visual Basic makes it easy to set up menus using the MenuStrip control. When dragged onto a form, the MenuStrip control appears as a box containing the words "Type Here," located in the upper part of the form. You can click the box and type inside it to create the menu titles.

When the title for one menu item is set, additional menu items can be created below and to the right of the first, allowing you to extend the menu with as many additional items or sub-items as you want. When the look of your menu is complete, you can create event handlers to handle the Click events for each item.

Try It!

To add a menu

  1. Make a new project with File>New Project.
     
  2. Click Windows Application and set the name to Menus before clicking OK.
     
  3. Resize the form to a smaller size, as shown below.
     
  4. From the Toolbox, drag a MenuStrip control onto the new form. You won't find the MenuStrip control in the Common Controls group - it can be found in the Menus and Toolbars group. Regardless of where you drop it, the MenuStrip control attaches itself to the uppermost part of the form. You may also have noticed that there is a MenuStrip1 icon added in a gray area below the form—this area is called the component tray. If you click outside of the MenuStrip control, it will disappear; you can bring it back by clicking on the MenuStrip1 icon.
     
  5. In the form, click the MenuStrip control, type File, and then press Enter. New boxes for additional menu entries appear below and to the right of the first menu item. These are spaces for additional menu items. You can continue to add menu items in either direction until your menu is complete.
     
  6. In the box beneath the first box, type Exit, and then press the Enter key.
     
  7. Double-click the Exit menu item to open the Code Editor.
     
  8. In the ExitToolStripMenuItem_Click event handler, type the following code.

       End
     

  9. Press F5 to run your program. Using the mouse, click the File menu, and then choose Exit. Your application closes.

Add a label to your form, name it lblSample, set its text property to 'Sample Text', font property to size 24, backcolor to light yellow,  borderstyle to fixed 3D, autosize to false, textalign to centred (both horizontally and vertically). Your form should now look something like this.

Now keep adding menu items to the menus until you have all the items shown in the following table. The Exit item should be at the bottom of the File menu but you included that before you included the Reset item (silly you) so you'll have to drag the Exit item above the Reset item.

File
     Reset
     Exit

Background
     Red
     Green
     Blue
     Random

Text
     Align
          Left
          Centre
          Right
    BorderStyle
          None
          Fixed Single
          Fixed 3D

Help
     About

The Text menu above has an extra layer, so that when you point at the Align menu item additional items open to the right of that, as shown here (this shows what it looks like in Run mode, not Design mode).

Now copy all the code below and paste it into your VB project, replacing ALL the code that is there now. Then fill in the missing blocks of code, as indicated by the comments in green (which you can remove).

Public Class Form1
 
Private Sub ResetToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ResetToolStripMenuItem.Click
     'Do this for yourself!
End Sub
 
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
     End
End Sub
 
Private Sub RedToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RedToolStripMenuItem.Click
     Me.BackColor = Color.Red
End Sub
 
Private Sub GreenToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles GreenToolStripMenuItem1.Click
     Me.BackColor = Color.FromArgb(255, 0, 255, 0)
End Sub
 
Private Sub BlueToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles BlueToolStripMenuItem.Click
     'Do this for yourself!
End Sub
 
Private Sub RandomToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RandomToolStripMenuItem.Click
     Randomize()
     Me.BackColor = ColorTranslator.FromOle _
(RGB(Rnd() * 256, Rnd() * 256, Rnd() * 256))
End Sub
 
Private Sub LeftToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles LeftToolStripMenuItem.Click
     lblSample.TextAlign = ContentAlignment.MiddleLeft
     LeftToolStripMenuItem.Checked = True
     CentreToolStripMenuItem.Checked = False
     RightToolStripMenuItem.Checked = False
End Sub
 
Private Sub CentreToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles CentreToolStripMenuItem.Click
     lblSample.TextAlign = ContentAlignment.MiddleCenter
     LeftToolStripMenuItem.Checked = False
     CentreToolStripMenuItem.Checked = True
     RightToolStripMenuItem.Checked = False
End Sub
 
Private Sub RightToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RightToolStripMenuItem.Click
     'Do this for yourself!
End Sub
 
Private Sub NoneToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles NoneToolStripMenuItem.Click
     lblSample.BorderStyle = BorderStyle.None
     NoneToolStripMenuItem.Checked = True
     FixedSingleToolStripMenuItem.Checked = False
     Fixed3DToolStripMenuItem.Checked = False
End Sub
 
Private Sub FixedSingleToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles FixedSingleToolStripMenuItem.Click
     lblSample.BorderStyle = BorderStyle.FixedSingle
     NoneToolStripMenuItem.Checked = False
     FixedSingleToolStripMenuItem.Checked = True
     Fixed3DToolStripMenuItem.Checked = False
End Sub
 
Private Sub Fixed3DToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Fixed3DToolStripMenuItem.Click
     'Do this for yourself!
End Sub
 
Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
     MsgBox("Copyright Nigel Ward, May 2008")
End Sub
End Class

The 'Reset' code should set the label's textalign property back to centred, the borderstyle to Fixed 3D and the form's backcolor to its original gray color which is given by RGB(236, 233, 216). It should also set the checked property of the text menu correctly.

 Some other points to note:

bullet

Be careful to spell the menu texts correctly otherwise things get confusing later on.
 

bullet

The text is initially centred so check the corresponding menu option by right-clicking it and choosing checked.
 

bullet

The label's borderstyle is initially Fixed 3D so check the corresponding menu option by right-clicking it and choosing checked.
 

bullet

If you need to delete a menu item then right-click it and choose Delete.
 

bullet

Note that a Randomize statement precedes the Rnd() function, as previously discussed.
 

bullet

If you want to change a property of the form by using code (in other words 'at Run time') then refer to the form as 'Me' instead of using its name (usually Form1).
 

bullet

Note that when a menu item is selected in the designer window (not the code window) its properties appear in the properties window, for a menu item is a control.
 

bullet

I have included a couple of comments to make things clearer. Anything following an apostrophe is considered to be a comment, provided the apostrophe is not part of a string (a string is a string of characters enclosed within double quotes). VB shows comments in green, and ignores them when the program is running. For complex programs it is very important to include plenty of comments, not just to help other people who might work on your program, but also to help yourself understand the program at a later date.

Colour

Your eyes have only three kinds of colour detecting 'cone' cells - one detects red, one green and one blue. Therefore every one of the millions of colours you can see can be obtained by adding together the right amounts of red, green and blue light. That's why if you look at your computer monitor or your TV you will see it consists only of red, green and blue dots - no other colours. Therefore we can represent any by indicating how much red, green and blue light is present in the colour. In the RGB (red, green, blue, always in that order) notation used by computer programmers, each of the three numbers can have a value between 0 and 255. Thus RGB(255,0,0) represents bright red for it contains maximum red, zero blue and zero green.

In the RGB representation, (0,0,0) represents black and (255,255, 255) represents white. Adding primary colours gives the secondary colours: red + blue = magenta, red + green=yellow, green + blue=cyan. This is demonstrated in the following diagram which shows what happens when circular red, green and blue light beams are projected onto a white screen and made to overlap:

Such a diagram is sometimes called a colour wheel. Colours on opposite sides of the colour wheel, such as green and magenta, are called complementary colours.

Some of the above statements, such as green + red = yellow, may surprise you, for your experience mixing paints may seem to contradict this. The explanation is that paint pigments absorb colours, leading to colour subtraction, whereas the above text and the colour wheel describe colour addition. Colour addition corresponds to the way your computer screen works - if the screen's tiny red and green dots are lit simultaneously then you will see yellow. Look again at the diagram, and make sure that you can tell the difference between red and magenta, and between blue and cyan. Note how the diagram correctly shows that when red, green and blue lights are added together, the result is white.

Can you tell what colour the RGB combination (0,255,255) would refer to? This would be a combination of no red + bright green + bright blue, in other words CYAN.

It used to be possible in VB to use the RGB notation in a form as simple as this (don't forget that 'Me' refers to the form:

Me.backcolor = RGB(255,0,0)

but things have become more complicated in recent versions of VB. Now the same statement would be written

Me.backcolor = ColorTranslator.FromOle(RGB(255, 0, 0))

There is some good news: VB also lets you refer to any of about a hundred standard colours by name, so the same statement could also be written

Me.backcolor = Color.Red

Standard colours include all the colours mentioned on this page (black, white, red, green, blue, magenta, yellow, cyan) as well as gray, lightblue, darkred etc (note the lack of spaces in these names). They forgot 'LightRed' and 'LightMagenta'  and 'DarkYellow', by the way.

If you want to use transparent colours then you can use ARGB notation - ARGB is the same as RGB except that it begins with an ALPHA number that represents TRANSPARENCY. Using ARGB the code to set a label's backcolor to semi-transparent red would be:

lblMyLabel.BackColor = Color.FromArgb(50, 255, 0, 0)

If you want to use a non-standard colour, though, you'll have to use the RGB notation mentioned earlier though. (There are yet more colour formats that you can read about in the Help system.)

 

VB6 vs. VB.NET

Previous Up Next