0

On VB.NET the multilingual resource for MenuStrip is not applied to the drop down down submenu . Take a look at the pic to know more clearly

If any mistakes in the questions , I apologize

Here is my code snippet

For Each item As ToolStripItem In MenuStrip1.Items
    If TypeOf item Is ToolStripDropDownItem Then
        For Each dropDownItem As ToolStripItem In CType(item, ToolStripDropDownItem).DropDownItems
            resources.ApplyResources(dropDownItem, dropDownItem.Name, New CultureInfo(Lang))
        Next
    End If
    If TypeOf item Is ToolStripMenuItem Then
        For Each child As ToolStripItem In CType(item, ToolStripMenuItem).DropDownItems
            resources.ApplyResources(child, child.Name, New CultureInfo(Lang))
        Next
    End If
    resources.ApplyResources(item, item.Name, New CultureInfo(Lang))
Next

The drop down sub menu is not changing

I have also tried this code but it seems nothing to change i.e i have cleared a small error and the output is the same as in the picture


            If TypeOf item Is ToolStripMenuItem Then
                For Each child As ToolStripMenuItem In CType(item, ToolStripMenuItem).DropDownItems
                    For Each dropDownItem As ToolStripItem In CType(item, ToolStripDropDownItem).DropDownItems
                        resources.ApplyResources(child, child.Name, New CultureInfo(Lang))
                    Next
                    resources.ApplyResources(child, child.Name, New CultureInfo(Lang))
                    Next
            End If
            resources.ApplyResources(item, item.Name, New CultureInfo(Lang))
        Next
  • 3
    The code doesn't go deep enough, it modifies child but not the child.DropDownItems. Recursion is the natural way to visit tree structures. – Hans Passant Jan 02 '20 at 12:09
  • @HansPassant Thank you for your comment, so do you have any idea about how to modify this code ? I have tried but it returns in error – Siva chidambaram Jan 02 '20 at 12:29
  • 1
    [WinForms localization. How to change the language of a Menu](https://stackoverflow.com/a/52179254/7444103) – Jimi Jan 02 '20 at 12:46
  • *"I have tried but it returns in error"*. Then edit your question to show us what you tried and what the error message was. That's how this site works. – jmcilhinney Jan 02 '20 at 12:48

1 Answers1

1

As @HansPassant suggested in the comments, you can use a recursive method to get the job done.

I also suggest you take a look at the article @Jimi posted.

Well, here's how to get it done recursively.

Sub ApplyLanguageChanges(ByRef item As Object)
    'Check for a MenuStrip first'
    If TypeOf item Is MenuStrip Then
        resources.ApplyResources(item Ctype(item, MenuStrip).Name, New CultureInfo(Lang))
        For Each child In Ctype(item, MenuStrip).Items
            ApplyLanguageChanges(child)
        Next
    'Next, check for a ToolStripDropDownItem'
    Else If TypeOf item Is ToolStripDropDownItem
        resources.ApplyResources(item CType(item,  ToolStripDropDownItem).Name, New CultureInfo(Lang))
        For Each child In CType(item,  ToolStripDropDownItem).DropDownItems
            ApplyLanguageChanges(child)
        Next
    'Finally, check for a ToolStripMenuItem'
    Else If TypeOf item Is ToolStripMenuItem
        resources.ApplyResources(item, CType(item, ToolStripMenuItem).Name, New CultureInfo(Lang))
     End If
End Sub

The Method above can be called like this:

ApplyLanguageChanges(MenuStrip1)
preciousbetine
  • 2,959
  • 3
  • 13
  • 29