Changing appearance of all menu items during runtime

Sokrates

New member
Joined
Feb 17, 2023
Messages
1
Programming Experience
5-10
Hello,
first: please excuse my limited English. I'll try my best.
I am moving from VB6 to VB2017 and I know how to change e.g. the colour of all buttons during runtime.
Dim C as Control
For Each C in Form.Controls
If TypeOf C = Button Then ....

Is it possible to do the same with menues? So if the user wants to make everything bigger or smaller, depending of the monitor.
I tried to substitute the expressions "Control", "Controls" and "Button" but didn't find a solution.

Who can help?
Regards
Sokrates from Germany
 
The MenuStrip is part of a rather large hierarchy of ToolStrip related classes. There are several item types, some of them has dropdowns that contains subitems, similar to how each control also has a Controls collection of child controls.

It may help to get an overview of ToolStrip Control Architecture - Windows Forms .NET Framework
You can access all the items in a ToolStrip through the Items collection. You can access all the items in a ToolStripDropDownItem through the DropDownItems collection.
Both Items and DropDownItems returns a ToolStripItemCollection, so this is suitable for a recursive method to drill down through all items.
ToolStripItem is the base class for all item types and has most of the item properties. Then you can check if the item is a ToolStripDropDownItem and process these items as well.
Here is very basic example of such method, which doesn't do anything except write the type Name and Text for each item.
VB.NET:
Private Sub ProcessItems(items As ToolStripItemCollection)
    For Each item As ToolStripItem In items
        'do something with this item
        Debug.WriteLine($"{item.GetType.Name}: {item.Text}")

        'check if there could be dropdown items
        Dim dropdownitem = TryCast(item, ToolStripDropDownItem)
        If dropdownitem IsNot Nothing Then
            'recursive call for dropdown items
            ProcessItems(dropdownitem.DropDownItems)
        End If
    Next
End Sub
Called like this for a MenuStrip: ProcessItems(SampleMenuStrip.Items)
It will work also for the other tool strip types, like ContextMenuStrip, StatusStrip and ToolStrip.

Check the properties available for ToolStripItem Class (System.Windows.Forms) for things that you may want to change. You can also follow the links for more specific classes (look at the Derived classes list), for example ToolStripDropDownItem > ToolStripMenuItem, and check if these have extra properties that you want to change for these cases.
 
Back
Top