adding menus to a visual basic

Upload: gianthea

Post on 10-Apr-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 Adding Menus to a Visual Basic

    1/36

    Adding Menus to a Visual Basic

    .NET Form

  • 8/8/2019 Adding Menus to a Visual Basic

    2/36

    Start a new project. To your new form, use the

    toolbox to add a MenuStrip control:

    Double click the control to add one to yourform. When you do, you'll notice two things.

    At the top of your form, you'll see this:

    . Examine the bottom of your screen, on the left. You'll see this:

  • 8/8/2019 Adding Menus to a Visual Basic

    3/36

    This is the control itself. If you click onthis (it's highlighted above), you'll seethat the Properties box on the rightchanges.

    To start building your menu, click insidethe area that says "Type Here". Type theword File:

    To create items on your File menu, click

    inside the Type Here box. Enter the wordNew, and press the enter key on yourkeyboard again.

  • 8/8/2019 Adding Menus to a Visual Basic

    4/36

    Your menu will then 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".

  • 8/8/2019 Adding Menus to a Visual Basic

    5/36

    To add a separator, click inside the blue "Type

    Here" box. Instead of typing a letter, type the

    minus character "-" (in between the "0" keyand the "+/=" key on your keyboard). When

    you hit your return key, you'll see the

    separator appear

  • 8/8/2019 Adding Menus to a Visual Basic

    6/36

    Adding code to a VB.NET menu

    Press F7 on your keyboard to go

    to the code window

    Click the black arrow at the top,where it says General:

    The Exit menu here is"ExitToolStripMenuItem".

  • 8/8/2019 Adding Menus to a Visual Basic

    7/36

    However, "ExitToolStripMenuItem" is

    very difficult to remember. We canrename our menu items so that they

    are more descriptive. So do this:

    Get back to your form by pressing

    Shift + F7 on your keyboard

    On the Property window, change theProperty name to mnuExit.

  • 8/8/2019 Adding Menus to a Visual Basic

    8/36

    Now press F7 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): you

    can see mnuExit.

    Now select the mnuExit and the event is click. There's only one line of code to add. It's this:

    Me.Close( )

    The word "Me" refers to the form. When yourtype the word Me, you'll see a list of items

    appear.

  • 8/8/2019 Adding Menus to a Visual Basic

    9/36

    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" ofNew

    Type an ampersand symbol (&)

  • 8/8/2019 Adding Menus to a Visual Basic

    10/36

    Add underlines for the "F" of you File menu,

    the "O" ofOpen, the "S" ofSave, and the "X"

    ofExit.

    Run your program and try your shortcuts.

  • 8/8/2019 Adding Menus to a Visual Basic

    11/36

    Key combination shortcuts

    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:

    Your Exit menu should have a Ctrl X shortcut

  • 8/8/2019 Adding Menus to a Visual Basic

    12/36

    A VB .NET Menu Project Add the following Main Menu items to the menu bar

    you have already designed in this section: Edit

    View

    On your Edit Menu, place the following menu items:

    Undo Cut

    Copy

    Paste

    On your View Menu, place the following menu items:

    View Textboxes

    View Labels

    View Image

  • 8/8/2019 Adding Menus to a Visual Basic

    13/36

    When you have finished, your menus

    should look like these

    Edit Menu

    View Menu

  • 8/8/2019 Adding Menus to a Visual Basic

    14/36

    Code for The Open File Dialogue Box

    First, place two textboxes on your form.In the properties box, locate the

    MultiLine property. It is set to False by

    default (which is why you can't changethe height of textboxes). Change this

    value to True.

    Type some default text for the TextProperty of textbox1. Change the Font

    size to 14 points.

  • 8/8/2019 Adding Menus to a Visual Basic

    15/36

    Open up your toolbox, and locate the

    control called "OpenFileDialog".

    Double click the control to add one to

    your project.

    But notice that the control doesn't getadded to your form. It gets added to the

    area at the bottom, next to your menu

    control:

  • 8/8/2019 Adding Menus to a Visual Basic

    16/36

    On the properties window. Click on the Name

    property and change the name to openFD

    Access the code for your File > Open menu

    item.

    The Initial Directory

  • 8/8/2019 Adding Menus to a Visual Basic

    17/36

    The Initial Directory

    openFD.InitialDirectory = "C:\"

    openFD.ShowDialog()

    Run your programme again, and see theresults in action. You should see the

    contents of the "C" folder on your hard

    drive (if you root folder is calledsomething else, change the code above).

  • 8/8/2019 Adding Menus to a Visual Basic

    18/36

    The Title Property

    openFD.InitialDirectory = "C:\"

    openFD.Title = "Open a Text File"

    openFD.ShowDialog()

    Run your code again, and Click File > Open

    from your menu. You should see this at the

    top of the Open dialogue box:

  • 8/8/2019 Adding Menus to a Visual Basic

    19/36

    The Open File Dialogue Box Filter

    Property

    We'll restrict our users to only opening Text files,those that end in the extension ".txt".

    The following code (in bold) shows how to use the

    filter property: openFD.InitialDirectory = "C:\"

    openFD.Title = "Open a Text File"openFD.Filter = "Text Files|*.txt"

    openFD.ShowDialog() Run your code. Click File > Open on your menu, and

    then click the arrow on the drop down box for "Filesof Type".

  • 8/8/2019 Adding Menus to a Visual Basic

    20/36

    You should see this:

    Or put this to see the one belowopenFD.Filter = "Text Files(*.txt)|*.txt"

  • 8/8/2019 Adding Menus to a Visual Basic

    21/36

    OpenFD.FileName

    However, this is a property that returns a

    value (a string value). The value is thename of a file. So you have to assign this

    value to something. We can assign it to a

    new variable:

    Dim strFileName As String

    strFileName = OpenFD.FileName

    The value in the variable strFileName will

    then hold the name of the file selected

  • 8/8/2019 Adding Menus to a Visual Basic

    22/36

    So change your code to this

    Dim strFileName As StringopenFD.InitialDirectory = "C:\"

    openFD.Title = "Open a Text File"openFD.Filter = "Text Files|*.txt"

    openFD.ShowDialog()

    strFileName = OpenFD.FileName

    MsgBox strFileName

  • 8/8/2019 Adding Menus to a Visual Basic

    23/36

    Run your programme, and click your File >

    Open menu. Navigate to where you have

    some text files. Click one to select it. Thenclick the Open button. You should see the

    name of the file displayed in your message

    box:

  • 8/8/2019 Adding Menus to a Visual Basic

    24/36

    The SaveFileDialog Control

    Double click this control to add one to your

    project. If you look at the bottom of the

    screen, you'll see the control added there,

    rather than onto your form:

  • 8/8/2019 Adding Menus to a Visual Basic

    25/36

    Changed the Name property of

    your control to something moremanageable. Change it to saveFD

    Access the code for your File >

    Save menu item. Then add the

    following code:

    saveFD.ShowDialog()

  • 8/8/2019 Adding Menus to a Visual Basic

    26/36

    Run your programme, then click your File >

    Save menu item. You should see the Save As

    dialogue box appear.

    Just like the Open control, you can use the

    properties of the Save control on your

    dialogue boxes. Try changing these properties,just like you did with the Open properties:

    Initial Directory

    TitleFilter

    FileName

  • 8/8/2019 Adding Menus to a Visual Basic

    27/36

    There's another useful property you can use

    with the Save control - the Overwrite prompt.

    When you set this property, a message boxpops up warning you that the file will be

    overwritten, and do you want to continue. To

    use this property, the code is this:saveFD.OverwritePrompt = True

    However, just like the Open box, when you

    click the Save button no file is actually beingsaved. You have to write your own code for

    this.

  • 8/8/2019 Adding Menus to a Visual Basic

    28/36

    Cut, Copy, Paste and Undo in VB .NET

    The Copy Menu If you type Textbox1 in your code window,

    then a full stop, you get a list of properties

    and methods available to the textbox. Scroll

    up to the top and locate the Copy method:

  • 8/8/2019 Adding Menus to a Visual Basic

    29/36

    Your code window should look something like

    this:

  • 8/8/2019 Adding Menus to a Visual Basic

    30/36

    The Paste Menu

  • 8/8/2019 Adding Menus to a Visual Basic

    31/36

    The Cut Menu

    Access the code for you Cut menu item. Add

    the following code to it:

    TextBox1.Cut()

    Run your program, and select the text in

    textbox one. From your menu, click Edit > Cut.

    The text should disappear (it's on the

    clipboard, though). Click inside textbox two,

    and click Edit > Paste. The text should bepasted over.

  • 8/8/2019 Adding Menus to a Visual Basic

    32/36

    The Undo Menu

    For the Undo menu, add this line of code:

    TextBox1.Undo()

    The Edit menu we implemented is only a simple

    one. But it does demonstrate what you can do

    with VB.NET and menus.

  • 8/8/2019 Adding Menus to a Visual Basic

    33/36

  • 8/8/2019 Adding Menus to a Visual Basic

    34/36

  • 8/8/2019 Adding Menus to a Visual Basic

    35/36

  • 8/8/2019 Adding Menus to a Visual Basic

    36/36