Previous | Index | Next 

[HOWTO] Move a control over a toolbar at runtime

One of the key differences between the VB6 and VB.NET Toolbar controls is that the former can work as a control container, whereas the latter can’t. (This topic is explained in more detail in this article.) This detail has a consequence on how you move a standard control – for example, a progress bar – over a toolbar. Such an action is typically accomplished under VB6 by means of the following code:

        Set ProgressBar1.Container = Toolbar1
        ProgressBar1.Move PlaceholderButton1.Left, PlaceholderButton1.Top, _
             PlaceholderButton1.Width, PlaceholderButton1.Height

where PlaceholderButton1 is a toolbar button whose Style property is set to 4-tbrPlaceholder.

Because the .NET Toolbar control can’t contain other controls, you can’t move a control over a toolbar by setting its Container property. If TextBox1 is a standard System.Windows.Forms.TextBox control, then the following statement

        TextBox1.Container = Toolbar1

makes the TextBox1 control disappear from its previous position but doesn’t make it reappear over the toolbar.

For this reason, assignments to the Container property of any control in VB Migration Partner’s support library throws an exception if the control being assigned is a Toolbar control. The message of the exception is

        The Toolbar control can’t work as a container

(The assignment is ignored and doesn’t throw any exception if the VB6Config.ThrowOnUnsupportedMembers property is False.)

Conveniently, the VB6Toolbar control exposes a custom method named MoveControlOverButton, whose effect is virtually identical to changing a control’s Container property so that its new container is the toolbar. The syntax of such a method is:

        Toolbar1.MoveControlOverButton control, button [, resize]

where control is the control to be moved, button is either the Button object or the index of the toolbar button over which the control must be moved, and resize is an optional Boolean value that specifies whether the control must be resized to take the same area as the placeholder button. For example, assuming that PlaceholderButton is the first button in the toolbar, you can move the ProgressBar1 control over the placeholder button named PlaceholderButton by means of one of these statements:

        Toolbar1.MoveControlOverButton ProgressBar1, 1, True
        Toolbar1.MoveControlOverButton ProgressBar1, PlaceholderButton, True

The best way to include this statement in the converted VB.NET application is by means of a ReplaceStatement pragma:

        Set ProgressBar1.Container = Toolbar1
        '## ReplaceStatement Toolbar1.MoveControlOverButton ProgressBar1, 1, True
        ProgressBar1.Move PlaceholderButton1.Left, PlaceholderButton1.Top, _
            PlaceholderButton1.Width, PlaceholderButton1.Height

Notice that the MoveControlOverButton method moves the control – ProgressBar1 in this example - “over” the toolbar, without making it a child of the toolbar control. If the toolbar is then moved, the ProgressBar1 control is moved in a synchronized way. However, if the placeholder button is moved or resized, you have to manually move or resize the ProgressBar1 control.

 

Previous | Index | Next