• home
  • forum
  • my
  • kt
  • download
  • How to make a menu: Part two

    Author: 2008-09-13 09:54:31 From:

    Firstly, to start off do my other tutorial, part one of how to make menus.

    When you've done that, we'll start :)

    So we'll get right into it now. We will start this tutorial by making an edit menu. Open up your project that you made with the last tutorial and we shall use that.

    Right click and go to the Menu Editor. Click in that white space under the exit button that we made. Now in the caption field, type in "&Edit". In the name field type in, "mnuEdit". We won't click the arrow on this one because that would add this menu item to the File menu. Instead we want to make this a seperate menu; with its own buttons in it. When you've done that, we want to add some buttons in. The usual buttons you would find in a Edit menu are, "Copy", "Cut", "Paste" and "Select all" so that's what we'll make. We will also make a seperator bar.

    Let's start :D

    Click under the edit menu that we made, on the white spot. Click the arrow again and in the caption, enter, "Cu&t". Under name enter, "mnuEditCut". Now the usual shorcut key for the Cut item is CTRL+X. This means that if you highlight some text and press CTRL+X it will cut the text and add it to the clipboard so you can paste it. Click on the listbox next to Shortcut Key and change it to CTRL+X:

    Now do the same for a copy, paste and select all menu. The names and captions should be:

    Copy:

    Caption: "&Copy" Name: "mnuEditCopy" Shortcut: "CTRL+C"

    Paste:

    Caption: "&Paste" Name: "mnuEditPaste" Shortcut: "CTRL+V

    Select All:

    Caption: "Select &All" Name: "mnuEditSAll" Shortcut: "CTRL+A"

    Step 5

    We now need to add a seperator bar in. We will seperate Select all from the other buttons. To do this, click on the whit space and in the caption area enter: "-". This tells the app that it is a seperator. There isn't another way to do it. In the name enter "mnuEditSep1" Remember to click the accross arrow. Now we need to move it up above the Select all button. Click the up arrow once and that will move it to the correct place.

    Now we need to change what the buttons do. We'll create a textbox firstly so we can use that for all our button thingies. So go to your toolbox on the left-hand-side and click on the textbox icon. Then build one on the form:

    Click on the textbox and change the name from the properties panel on the right-hand side to "txtInput". Change the text to anything you like:

    Time to code :D

    Click on the edit menu and select the Cut button:

    You should get a white screen with your code. Find this piece of code:

    Private Sub mnuEditCut_Click()

    End Sub

    In the middle of this, insert this code

    Clipboard.Clear 'clears clipboard
    Clipboard.SetText Screen.ActiveControl.SelText 'adds text to clipboard
    Screen.ActiveControl.SelText = "" 'deletes selected text

    You should now have this code

    Private Sub mnuEditCut_Click()
    Clipboard.Clear 'clears clipboard
    Clipboard.SetText Screen.ActiveControl.SelText 'adds text to clipboard
    Screen.ActiveControl.SelText = "" 'deletes selected text
    End Sub

    What this is telling the application to do is, when the menu button is pressed is to clear the clipboard. This means anything that was on CTRL+C will be gone and replaced with new text. Then it tels the app to set the clipboard text to any selected text on the form. And lastly it deletes the selected text from the textbox. We do this because the Cut button does that :P

    Do the same thing for the Copy button but the code should be:

    Clipboard.Clear 'clears clipboard
    Clipboard.SetText Screen.ActiveControl.SelText 'adds selected text to clipboard
     Once again this clears any text that has been CTRL+c'd. Then it sets the text to any selected text.

    Now do the same for Paste. The code is:

    Screen.ActiveControl.SelText = Clipboard.GetText 'pastes the text   This code goes to the current textbox and inserts the text on the clipboard.

    And do the same for the Select all button. The code is:

    Screen.ActiveControl.SelStart = 0 'starts selecting the text at the start of the textbox
    Screen.ActiveControl.SelLength = Len(Screen.ActiveControl) 'selects up to where the text ends
      This code goes to the active text box, sets the cursor position to the start and then selects all of the text using the len() function.

    Now press F5. This will run your application. Alternitivly you can press this button (the one highlighted in blue):

    When your app is up and running you should be able to use your menu with the text box we created.

    When you are done with your application, exit out of it and save.

     

    Download Project

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      .NET (8)
      Buttons (3)
      Database Related (7)
      Date and Time (1)
      Development (3)
      Error Handling (2)
      File Manipulation (5)
      Introduction to Visual Basic (24)
      Miscellaneous (3)
      Multimedia (10)
      Networking (10)
      Security (1)
      VB Script (6)

    New

    Hot