ProGrammerZ
Welcome to ProGrammerZ™️ and to our forum.
Freely explore and discover in our forum by Registering for a new account for free!
By logging in to our forum, you can see a lot more information in what you are viewing.
Please Click "Register" if you don't have an account yet.

-ProGrammerZ

Menus in Visual Basic

View previous topic View next topic Go down

Menus in Visual Basic

Post by BlackZ on Sat Jan 26, 2013 11:22 am

Adding Menus to a Visual Basic .NET Form

In this section we’ll see how to add menus to a Visual Basic .NET form. These type of menus are very common to Windows programme. Visual Basic itself has many of these drop down menus - File, Edit, View, Project, Format, etc. And they’re very easy to add.



Start a new project. To your new form, use the toolbox to add a MenuStrip control:



Double click the control to add one to your form. When you do, you'll notice two things. At the top of your form, you'll see this:



We'll see how to construct our menu soon. But notice the other things that gets added to your project. Examine the bottom of your screen, on the left. You'll see this:



This is the control itself. If you click on this (it's highlighted above), you'll see that the Properties box on the right changes. There are many properties for the control. But there are lots of properties for the MenuItem object. The MenuItem object is the one at the top of the form - The one that says Type Here.

To start building your menu, click inside the area that says "Type Here". Type the word File:



Now press the enter key on your keyboard. Your menu will look like this:



To create items on your File menu, click inside the Type Here box. Enter the word New, and press the enter key on your keyboard again. Your menu will then look like this:



Add an "Open" and a "Save" item to your menu in the same way. It should look like this:



The final item we'll add to our menu is an "Exit" item. But you can add a separator between the "Save" and "Exit".

To add a separator, click inside the blue "Type Here" box. Instead of typing a letter, type the minus character "-" (in between the "0" key and the "+/=" key on your keyboard). When you hit your return key, you'll see the separator appear:



Click inside the "Type Here" area, and add an Exit (or Quit) item. Click back on your form, away from the menu, to finish off. You should now have a File menu like this one:



To see what your menu look like, Run your programme. Click the File menu. We haven't added any code to the menu yet, so nothing will happen if you click an item on the menu. But it does look quite good. Very professional!


In the next part, we'll see how to add some code to the Quit menu.

([You must be registered and logged in to see this link.])



[You must be registered and logged in to see this link.]
avatar
BlackZ
Owner
Owner

Posts : 97
Join date : 2012-03-03
Location : Philippines

http://programmerz.tk

Back to top Go down

Re: Menus in Visual Basic

Post by BlackZ on Sat Jan 26, 2013 11:27 am

Adding code to a VB.NET menu

This tutorial assumes that you have completed the previous one.

Stop your programme and return to the design environment. Click File in Design Time to see your drop down menu. You can double click an item to open up the code window. But don't do that yet.

Another way to get to the code for an object is this:

•Press F7 on your keyboard to go to the code window (CTRL +ALT + 0 in version 2012)
•Click the black arrow at the top, where it says General:



The Exit menu here is "ExitToolStripMenuItem". If you were to click that item, a code stub would open, ready for you to type your code.

However, "ExitToolStripMenuItem" is very difficult to remember. We can rename our menu items so that they are more descriptive. So do this:
•Get back to your form by pressing Shift + F7 on your keyboard
•Click the File menu to select it
•Select your Exit (or your Quit) item (Careful not to click in the middle as this may open the code window.Click near the left edge somewhere.)
•When you have the Exit item selected, look at the properties box on the right:



•Click inside the Name property
•Change it to mnuExit (or mnuQuit)
•Press your return key on your keyboard to confirm the change
Now press F7 (or CTRL + ALT + 0) again to bring the code window up. Click the drop down arrow of the General box, and you should see the new name appear (Notice that MenuItem6 has vanished):



Click on your new mnuExit item.

Nothing will happen!

To jump straight to the code, you need to look at the drop down box opposite. It will probably say "Declarations". Click the arrow and you'll see a new list:



The items in the Declarations box are called Events. The Event you want is the Click event. So select that one from the list (we'll cover Events in more depth later). When you select Click from the list, you are taken straight into the code for that event. It should be like this one:



The code above has been tidied up to fit on this page; yours will all be on one line. But notice that it says mnuExit_Click.

Don't worry too much about what it all means; we'll get to that in a later section. What we want to do is add some of our own code, so that out Exit menu item actually does something.

There's only one line of code to add. It's this:

Me.Close( )

The word "Me" refers to the form. When your type the word Me, you'll see a list if items appear. Double click the word Close, then press your return key. Your code window should look like this:



(Again this has been tidied up to fit on this page.)

To test out your new code, run your programme. Click your File menu, and then click the Exit item. Your form should close down, and you'll be returned to the design environment.


In the next part, we'll see how to add Sub menus.

([You must be registered and logged in to see this link.])



[You must be registered and logged in to see this link.]
avatar
BlackZ
Owner
Owner

Posts : 97
Join date : 2012-03-03
Location : Philippines

http://programmerz.tk

Back to top Go down

Re: Menus in Visual Basic

Post by BlackZ on Sat Jan 26, 2013 11:32 am

Add a Sub Menu to your VB.NET Form

This tutorial assumes that you have completed the first one.

A sub menu is one that branches of a menu item. They usually have an arrow to indicate that there's an extra menu available. You will have seen these plenty of times in Windows programmes.

You can create our own sub menus quite easily. Try this:

•Return to the Form view (Shift + F7 is a shortcut)
•Click on your File menu so that you can see it all
•Select the New item (Careful where you click. Click once on the left edge). You should see this:



•Click on the "Type Here" just to the right of New
•You'll see yet more "Type Here" areas:



•Type New Project, and then hit the return key on your keyboard
•Type in New File and then click away from the menu, somewhere on the form
•You will then have a menu like this one:



Save your work, and then run your programme. Click your new menu to see the following:



Of course, none of the menu items work except the Exit menu. But you should have found that adding menus to your programmes is an easy matter with VB.NET.

One more thing we can do. If you look closely at a lot of menu items, you see that they have shortcuts attached. There's two types of shortcuts: An underline shortcut, and a key combination shortcut. We'll see how to do that in the next section.

([You must be registered and logged in to see this link.])



[You must be registered and logged in to see this link.]
avatar
BlackZ
Owner
Owner

Posts : 97
Join date : 2012-03-03
Location : Philippines

http://programmerz.tk

Back to top Go down

Re: Menus in Visual Basic

Post by BlackZ on Sat Jan 26, 2013 11:40 am

Add Shorcuts to your Menu Items


This tutorial assumes that you have completed the first one.

In the previous tutorial, you learned how to add Sub Menus to your VB .NET forms. In this part, you'll see how to add shortcuts to your menu items


Underline Shortcut

To add an underline, do this:

•Click on your New menu item once. This will select it
•Position your cursor before the "N" of New
•Type an ampersand symbol (&)



•Hit the return key on your keyboard
•You should see this:



Notice that "N" of New is now underlined. If you want an underline shortcut, the ampersand character should be typed before the letter you want underlined.

Add underlines for the "F" of you File menu, the "O" of Open, the "S" of Save, and the "X" of Exit. When you're done, your menu should look like this one:



Time to see if your shortcut works. Run your programme. To use the underline shortcuts on menus, you first hold down the Alt key on your keyboard. Then type the underline character.

•Hold down the Alt key while your programme is running (You might not be able to see the underlines until you press the Alt key.)
•Press the letter "F" on your keyboard
•Then press the letter "X" (for the Exit menu)
•Your programme should close down
•All that and you didn't have to write a single line of code!


Key combination shortcuts

A key combination shortcut is one that appears at the end of a menu item (Ctrl + X, for example). You can easily add this option to your own programmes. So try this:

•In Design time, select the Exit item on your menu
•Look at the properties box on the right
•Locate the ShortcutKeys item:



•Click the down arrow to reveal the following:



The Modifier is the key you press with your shortcut. For example, the CTRL key then the "X" key on your keyboard. Place a check inside the Ctrl box. Then select the letter "X" from the Key dropdown list, as in the next image:



Click back on your menu to see what it looks like:



Run your programme and test out the shortcut. Don't click the File menu. Just hold down the Ctrl key on your keyboard. Then press the letter X. Again, the programme will close down.

You can add these types of shortcuts to any menu item. Just remember to choose a different key stroke combination for each one. And don't have too many of them - they'll spoil the look of your menu!



Now that you know how to add Menus, Sub Menus and Shortcuts it's time for you to complete your own menu bar. Click the project below to see what you need to do. It's not very difficult! In the section after the project.

([You must be registered and logged in to see this link.])



[You must be registered and logged in to see this link.]
avatar
BlackZ
Owner
Owner

Posts : 97
Join date : 2012-03-03
Location : Philippines

http://programmerz.tk

Back to top Go down

Re: Menus in Visual Basic

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum