Programming language Visual Basic 6 (Chapter 10 - Using the Menu)

Menu in Windows is where all the commands of a program arranged by type in order to make us easy.

There are two common types of menu is:  drop-down (drop down)  menu and  pop-up (up)  menu. We use drop-down menu to menu for main program. Usually it is located above the tip of the screen.Horizontally along the Menu Bar, if you click on a command in the Menu Bar, the program will drop down a menu MenuItems along with the vertical. If you click on any MENUITEM small triangle marked on the right, the program will popup a menu as shown in the picture below (when I click Format | Make Same Size ):
This image has been resized in order to avoid breaking the interface. Click here to view photo in full size (548x258)

Main Menu

We use  Menu Editor  to create or edit a Menu for the program. Menu belonging to a Form. Therefore, first we select a Form Designer to work with it (and not code of Form). Then we use the Menu command  Tools | Menu Editor  or click the icon on the toolbar of the Menu Editor Menu Editor to make out.

This image has been resized in order to avoid breaking the interface. Click here to view photo in full size (512x210)

The first is a blue streaks in the white frame of the Menu Editor, which displays the Command Menu Caption of the form first. When I type  & File  on textbox  Caption , it also appears on the blue trail above.Then, you can type the name of the textbox Menu Command  Name . Menu Command for a while but the name but I rarely use it, except that it visible / invisible (appear / disappear). Normally we use the name of MenuItems more.

For a menu as shown in the picture below we will have to edit add MenuItems Open, Save, Close and Exit.

The following figure shows all of the Menu Command File MenuItems are all indented to the right with four dots (....) in the front. When you click through to the sign pointing're MENUITEM Edit will have four dots, which is indented one level in the menu (nested).
Similarly, when you click the sign pointing to the left're MENUITEM Edit will take four dots, a level that is protruded in the Menu.

If the user wants to use the Alt key to use the menu, type add a  &  before the character you want in the Caption menu. Eg  Alt-F  will drop down the File Menu of the Menu Command.

If you give MENUITEM  & Open  called  mnuOpen , when you click on it on the Form Caption during design, VB6 IDE will display the shell of the  Sub mnuOpen_Click () , like Sub cmdButton_Click () of a CommandButton:

Private Sub mnuOpen_Click()
    MsgBox "You clicked mnuOpen"
End Sub 
In the above example we review a Statement to display a simple message  "You clicked mnuOpen" . You can put a name MENUITEM whatever, but people often use the prefix mnu  to easily distinguish a MENUITEM Event Event with a CommandButton. Therefore, we have the name mnuFile, mnuOpen, mnuSave, mnuClose, mnuExit.

The dash between the Close and Exit MenuItems called  Menu Separator . You can insert a Separator Menu for Caption it by using  the minus (-) .

In addition to the Alt key you can also use the Shortcut to User's MENUITEM. In order for MENUITEM a shortcut, select it from a ComboBox Shortcut  Shortcut  Menu Editor.
In the picture below we choose  Ctrl + O  to mnuOpen.

By default, the Enabled and Visible MENUITEM. Now you can design for MENUITEM initial value of the Enabled and Visible and Enabled checkboxes using Visible.
While running program (at runtime), you can also change the values Enabled and Visible are as follows:

mnuSave.Enabled = False 
mnuOpen.Visible = False 
When a MENUITEM Enabled = False it is blurry and the user can not be used.

You use these marks up and down arrows to move MENUITEM was selected up and down the list of MenuItems. You use the button  Delete  to cancel MENUITEM has been selected,  Insert  to insert a new right on MENUITEM MENUITEM were selected and  Next  to select the right under MENUITEM MENUITEM has been selected.

Pop-up Menu

For the User, are working with an object in Windows means we can do is display the  Context Menu (Menu situations apply to the right) with one mouse click. Usually it is the Right Click Context Menu and is also known as  Pop-up Menu . Pop-Up menu, the very fact of a drop-down menu Command Menu Bar.Normal Command Menu Bar visible or invisible it may be (the disabled).

In the picture below, the User Right click on the form, mnuEdit will appear. If you do not normally want to use it in the Main User Menu, you give it invisible:

Popup menu code to make-up is written in an object's mouseDown event that happened here is that of the female form:
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) 
   ' Popup the Edit Menu if User clicked the Right Button of the Mouse
   If Button = vbRightButton Then 
      PopupMenu mnuEdit 
   End If 
End Sub 
Even if you want to mnuEdit normally invisible, you should also be visible in order for it to progress at the code on Click Events for the treatment of such mnuCopy mnuEdit MenuItems belong, and mnuPaste mnuCut.

Settings menu in the Registry contains

User billion program to fake an Option WordWrap you like this:

Option Program you want to remember that User has chosen, so next time the program startup User Option WordWrap also retains the same value.

The most convenient way is to store a value of Option WordWrap  Key  in the  Registry . Registry is a special database of Windows Operating System used to store the data related to Users, Hardware, Configurations, etc. .. ActiveX Components used in the computer. In the Registry, data is arranged according to each class. You can directly edit the value by using Registry Keys in the Registry Editor .
This image has been resized in order to avoid breaking the interface. Click here to view photo in full size (694x276)

In this program we also captured by the way always remember the location of the program when the program stopped Form, next to the User boot program, the program will initially position the same as before.

I will use to contain Checked Sub SaveSetting value of mnuWordWrap and Left, Top ofForm. I'll leave that code in the Sub Form_QueryUnload because it will be before theForm Unload executed.

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) 
   SaveSettings 
End Sub 

Private Sub SaveSettings() 
   ' Save Location of the form
   SaveSetting App.Title, "Location", "Left", Me.Left 
   SaveSetting App.Title, "Location", "Top", Me.Top 
   ' Save the setting of WordWrap in menu
   SaveSetting App.Title, "Settings", "WordWrap", mnuWordWrap.Checked 
End Sub 
Title of the program is App.Title. Usually it is the name of the VB Project, but you can milk it in the Project Property Dialog  (Tab  Make ):

When the container value of something (we call  Key ) to the Registry, you can arrange for it in  Sectionsole discretion. Here we set out two Sections name  Location  to hold the Top, Left of the Form and name settings  to accommodate mnuWordWrap.Checked Key.

Want to have the value of the program stored in the Registry Keys when it starts I just use  GetSetting Function  in  Sub Form_Load  to read in from the Registry as follows:

Private Sub Form_Load() 
   ' Initialise Location of the form by reading the Settings from the Registry
   Me.Left = Val(GetSetting(App.Title, "Location", "Left", "0")) 
   Me.Top = Val(GetSetting(App.Title, "Location", "Top", "0")) 
   ' Initialise setting of WordWrap in the menu
   mnuWordWrap.Checked = ( GetSetting(App.Title, "Settings", "WordWrap", "False") = "True" )
End Sub 
At first you do not have anything in the Registry, the  "0"  (string "0" is converted by Val to 0) is the default value for the Left and Top, Left  "False"  is the default value of mnuWordWrap.Checked.

In addition we also want to remember the name of the third program uses the most recent User Files. Ie in the Drop-down Menu Command File will be MENUITEM  Recent Files  to display Files from one to three names, the latest on the end. First, we need to create 3 SubmenuItem but bearing the same name mnuRFile  Index  by 0.1 and 2 (you type into the textbox  Index ). Captions we will use them to display names of the files. Filename not have any at the MENUITEM  Recent Files  will be dimmed (ie mnuRecentFiles.Enabled = False).

I shall contain the name as a String in the Files Section of the Registry Settings. Iseparate the files by name delimiter character |. Example: "LattestFileName.txt|OldFileName.txt|OldestFilename.txt"
Each time a User Open File name File we'll add it into the Registry at any time and keep only the names of three new Files use most.

Here is the code used to add new file names for most in the Registry:

Private Sub mnuOpen_Click() 
   ' Initialise Folder in Common Dialog
   CommonDialog1.InitDir = App.Path 
   ' Launch the dialog
   CommonDialog1.ShowOpen 
   ' Save the Filename in the Registry, using Object myRecentFiles
   myRecentFiles.AddFile CommonDialog1.FileName 
End Sub 

In Sub Form_Load code used to read and display the name RecentFiles Menu:
'
   Set myRecentFiles = New clsRecentFiles 
   ' Pass the form handle to it
   ' This effectively loads the most recently used FileNames to menu
   myRecentFiles.Init Me 

We will use a Class name  clsRecentFiles  to special care of the Registry Files containing names and display them in the name of the Files Menu. Inside I could clsRecentFiles  clsString , is a Class String song helps us break out in the name of the Registry Files based on the delimiter character where  | .
' Author: Le Duc Hong http://www.vovisoft.com
' Class Name: clsRecentFiles
' This Class saves the most Recent FileNames used in the Registry in form of
' a String delimited by |.
' Up to MaxFiles Filenames maybe stored.
' You need to pass the Form that contains the menu to it.
' The assumption is that you have created an array of MenuItems named mnuRFile
' to display the FileNames
'
Const MaxFiles = 3  ' Maximum number of FileNames to remember
Private myForm As Form 
Private RecentFiles As clsString 
Public Sub Init(TForm As frmMenu) 
   Set myForm = TForm 
   Set RecentFiles = New clsString 
   ' Read the Most Recent Filename String from the Registry
   RecentFiles.Text = GetSetting(App.Title, "Settings", "RecentFiles", "") 
   ' Assign the Delimiter character and tokennise the String (i.e. split it) into FileNames
   RecentFiles.Delimiter = "|" 
   UpdateMenu 
End Sub 

Public Sub AddFile(FileName As String) 
   ' Add the latest FileName to the list and update the Registry
   ' Prefix the FileName to the existing MostRecentFileName String
   RecentFiles.Text = FileName & "|" & RecentFiles.Text 
   ' Discard the oldest FileNames if the total number is greater than MaxFiles
   If RecentFiles.TokenCount > MaxFiles Then 
      Dim TStr As String 
      Dim i As Integer 
      ' Reconstitute the String that contains only the most recent MaxFiles FileNames
      For i = 1 To MaxFiles 
         TStr = TStr & RecentFiles.TokenAt(i) & "|" 
      Next 
      ' Remove the last delimiter character on the right
      RecentFiles.Text = Left(TStr, Len(TStr) - 1) 
   End If 
   ' Update the String in the Registry
   SaveSetting App.Title, "Settings", "RecentFiles", RecentFiles.Text 
   UpdateMenu 
End Sub
 
Private Sub UpdateMenu() 
   ' Display the most recent Filenames in the menu
   Dim i As Integer 
   ' If there is no FileNames to display then disable the MenuItem entry
   If RecentFiles.TokenCount = 0 Then 
      myForm.mnuRecentFiles.Enabled = False 
      Exit Sub 
   Else 
      ' Otherwise enable the MenuItem entry
      myForm.mnuRecentFiles.Enabled = True 
   End If 
   ' Assign FileName to Caption of mnuRFile array and make the MenuItem elements visible
   For i = 1 To RecentFiles.TokenCount 
      myForm.mnuRFile(i - 1).Caption = RecentFiles.TokenAt(i)  ' Assign to Caption
      myForm.mnuRFile(i - 1).Visible = True  ' Make the MenuItem visible
      If i = MaxFiles Then Exit For  ' This line maybe unnecessary
   Next 
   ' Make the rest of the MenuItem array mnuRFile invisible if there are less than MaxFiles
   If RecentFiles.TokenCount < MaxFiles Then 
      For i = RecentFiles.TokenCount To MaxFiles - 1 
         myForm.mnuRFile(i).Visible = False 
      Next 
   End If 
End Sub 
You can run RegEdit Command Line after clicking Start | Run

to see more of the Keys that program was contained in Sections  Location  and  Settings  FolderHKEY_CURRENT_USER \ Software \ VB and VBA Program Settings \ Menu
This image has been resized in order to avoid breaking the interface. Click here to view photo in full size (809x515)

Bạn có thể download source code của program mẫu nầy kể cả class clsRecentFiles.
(Collected)

0 comments:

Post a Comment