Previous | Index | Next 

[INFO] Arrays of menu controls can’t contain separators

VB Migration Partner supports arrays of menu controls: the program being converted can define an array of one or more elements at design time and can extend it at runtime by dynamically loading new elements. These arrays are converted into VB6ControlArray(Of VB6Menu) objects.

In VB6, a menu separator is a regular menu item whose Caption property is equal to “-“ (dash character); VB Migration Partner converts these separators into System.Windows.Forms.ToolStripSeparator objects. This conversion can occur even at runtime: in other words, VB6 code that transforms a standard menu item into a separator by assigning a “-“ character to the menu’s Caption property works fine after the conversion to VB.NET. The opposite operation – that is, transforming a separator into a regular menu item by assigning a different string to the Caption property – is also fully supported.

The only problem of menu separators is that they can’t be part of a menu array. This happens because ToolStripSeparator objects can’t belong to a VB6ControlArray objects that contains VB6Menu objects.
 
When migrating a menu array that contains a separator, the separator is discarded from the control array even if it retains the correct value for the Index property. The array can therefore have “holes”, thus it is recommended that you iterate over elements of a menu control array by means of a For Each loop rather than a For loop.

Another subtler malfunctioning occurs when your code processes all the elements in the control array and finds fewer elements than it expects because one or more separators have been removed from the control array.

The fact that VB Migration Partner discards separators from menu arrays becomes important if the separator was the only element of the array at design-time. (Such a menu array could be useful if the VB6 application extends the menu at runtime with information taken from a configuration file.) In such a case, the converted VB.NET application can’t compile because of the following error:
     'arrayname' isn’t a member of 'formname'
The only solution to this issue is revising the VB6 code so that the menu array contains at least one regular, non-separator item.

As explained previously, you can convert a regular menu item into a separator at runtime even if the menu item belongs to an array. For example, the following code works fine after the conversion to VB.NET:

        ' mnuArray is a control array of menu items
        Dim i As Integer
        For 1 = 0 To 15
            Load mnuArray(i)
            ' add a separator every three elements
            If (i Mod 4) = 0 Then
                mnuArray(i).Caption = "-"
            Else
                mnuArray(i).Caption = "Item #" + i
            End If
            mnuArray(i).Visible = True
        Next

This feature offers a workaround for the limitation that prevents you from adding a separator to an menu array: at design time you can define a menu that contains only standard elements, then you write code in the Form_Load event handler to assign a “-“ string to the Caption property of the elements that must become menu separators.

 

Previous | Index | Next