Looping through controls on a form but MenuItems are ignored

Hi,

Have an application where a form is sent to a sub where a text is assigned to all controls on that form. The header for that sub looks like this:

Public Sub SetResource(ByVal Form As System.windows.forms.Form)

We use a for each to loop through all controls in that form like this:

For Each oControl In Form.Controls

With the code

If oControl.GetType.Equals(GetType(Label))...

we check for different kind of controls. But when we check for MenuItems it does not work.

If oControl.GetType.Equals(GetType(MenuItem)

How can I check for menuitems

Best
/M



Answer this question

Looping through controls on a form but MenuItems are ignored

  • Ekta

    Basically, you will need to know if the current control is a container or a parent widget then you will have to scan its children separately.

    If oControl.GetType.Equals(GetType(ContextMenu)) Or If oControl.GetType.Equals(GetType(MainMenu))
      // loop thru the menuitems 

     


  • hadjici2

    Sorry, that does not work. No MainMenu exist among the controls in the for each loop, only some buttons, labels and a MDIClient. I have set breakpoints to examine all controls in the loop and no mainMenu or MenuItems appear

    The form in question has the MDIContainer property set to true and is calling the SetResource sub from its Load event to set all controls text to specified language.

    Is MenuItems a control
    Should the function be called from the Form_Load event

    Best

    /M


  • Ather.

    Thank you for your answer... I have tried to loop through the MenuItems in the menu by using this recursive function. The oGui.getString function retrieves a value from a ini file based on the menu item's name. The problem is that I can't get the menuitems name from the code... Shouldn't that be possible

    Public Sub SetResourceOnMenu(ByVal oMenu As Menu)
     
    For Each oMenuItem As MenuItem In oMenu.MenuItems
        oMenuItem.Text = oGUI.getString(oMenuItem.  <name does not exist>  )
        SetResourceOnMenu(oMenuItem)
     
    Next
    End Sub

    This problem regarding name, is it the same for ContextMenus

    Any suggestions

    Best
    /M


  • Alex974

    Unfortunately yes, You will have to really on the Text property of the menu-items. This is the same for Context-Menu.


  • mgee16

    Thank you for your answers... You were very helpful

    /M


  • I am Josha

    I've used the text property of the menu item...it does not have a Name property. Perhaps you can use that.
  • PhilippCH

    The main menu does not exist in the controls collection.

    Try looking for a MainMenu object for the form. If there is one, loop through the menu items. Here's an example in c# - I'm not well versed in vb but I hope it helps.

    MainMenu obj_Menu = this.Menu;

    if(obj_Menu != null)
    {
    for(i = 0; i < obj_MainMenu.MenuItems.Count; i++)
    {
    //do something
    }

    //or foreach

    foreach(MenuItem obj in obj_Menu.MenuItems)
    {

    //do something
    }

    }

    Good luck!


  • Looping through controls on a form but MenuItems are ignored