Adding menu items at run-time

I want a SIMPLE, EASY, and CLEAN way to add menu items at run-time.
I have the menu "Languages" that will contains a variable number of items, depending on the number of languages set by the program.
So, let say this number is set to 3 (English, French and Spanish), I want the program to add these 3 languages as menu-items at run time.

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

Erick37Connect With a Mentor Commented:
This will create new sub-menu items provided you already have one sub-menu (named mnuLangs) with an index of 0.
mnuLangs is the sub-menu of the top level Languages item.

Private Sub Form_Load()
    'Create new sub-menu entries
    'mnuLangs(0) already exists as "English"
    Load mnuLangs(1)
    mnuLangs(1).Caption = "French"
    Load mnuLangs(2)
    mnuLangs(2).Caption = "Spanish"
End Sub

Private Sub mnuLangs_Click(Index As Integer)
    Select Case Index
    Case 0
        Debug.Print "English"
    Case 1
        Debug.Print "French"
    Case 2
        Debug.Print "Spanish"
    End Select
End Sub
Bob LearnedCommented:
You could use the Load method.  With one menu sub-item in Languages mnuLanguage(0).  Set caption to "English".  Load new menu items:

Load mnuLanguage(1)
mnuLanguage(1).Caption = "French"
mnuLanguage(1).Visible = True

Load mnuLanguage(2)
mnuLanguage(2).Caption = "Spanish"
mnuLanguage(2).Visible = True

This could become a more complicated routine, but is this what you want?
SylvaniaAuthor Commented:
Yes, I had that in tought, but I want to handle an infinite languages possibility... altough I know there is not such a number of 'em down there! lol
Let say I just want to know the clean way to do the job, without hidding anything; just ADDING menu-items at run-time.

Bob LearnedCommented:
The possibilities for doing are almost infinite.

Something simple, like:

Private Sub LoadLanguages()

Dim vLanguages As Variant
Dim iMenu As Integer

   vLanguages = Array("English", "French", "Spanish", "German", "Italian")

   For iMenu = LBound(vLanguages) To UBound(vLanguages)

      If iMenu > LBound(vLanguages) Then
         Load mnuLanguages(iMenu)
      End If
      mnuLanguages(iMenu).Caption = vLanguages(iMenu)
      mnuLanguages(iMenu).Visible = True
   Next iMenu
End Sub

You could add each language to the array as needed.

You could also use databases and tables.

When using the Load statement, the new menu items will not be visible, so you need .Visible = True
SylvaniaAuthor Commented:
Thanks to you too, The learned one!
I just can't believe it was this simple!
With all the people saying to use api, and other stuff... the old LOAD was the key! lol

thanks pals!
All Courses

From novice to tech pro — start learning today.