The ToolBar control is recreated when you set the properties of the button on the ToolBar at runtime (820641)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

SYMPTOMS

You add a button to the ToolBar control on a Microsoft Windows form, and then you set the Style property of the button to DropDownButton. If you set any of the following properties of the button on the ToolBar at runtime, the ToolBar control is re-created:
  • ImageIndex
  • Text
  • Visible
  • Style
Note There may be some flickering as the toolbar is recreated at runtime.

STATUS

This behavior is by design.

MORE INFORMATION

The ToolBar control is not re-created when you set the Style property of the button on the ToolBar to PushButton or to ToggleButton. The ToolBar is re-created when you change the Style property, regardless of the Type property.

Steps to Reproduce the Behavior

  1. Start Microsoft Visual Studio .NET.
  2. Create a new Windows application by using Microsoft Visual Basic .NET or Microsoft Visual C# .NET.

    By default, Form1 is created.
  3. Add a ToolBar control to Form1.
  4. Right-click ToolBar1 on Form1, and then click Properties.
  5. In the Properties dialog box, click the Buttons property, and then click the ellipsis button (...).
  6. In the ToolBarButton Collection Editor dialog box, click Add.
  7. Set the Style property of ToolBarButton1 to DropDownButton.
  8. Set the Text property of ToolBarButton1 to UpdateButton.
  9. Click OK.
  10. Add a Button control to Form1.
  11. Add the following code to the Button1_Click event handler.

    Visual Basic .NET Code
    'Set the Text property of the ToolBarButton.
    ToolBar1.Buttons(0).Text = "ChangedText"
    Visual C# .NET Code
    //Set the Text property of the ToolBarButton.
    toolBar1.Buttons[0].Text= "Changed Text";
  12. If you are using Visual C# .NET, expand Windows Form Designer generated code, and then add the following code at the end of the InitializeComponent procedure:
    this.toolBar1.HandleCreated  += new System.EventHandler(toolBar1_HandleCreated);
    this.toolBar1.HandleDestroyed  += new System.EventHandler(toolBar1_HandleDestroyed);
  13. Add the following code after the Button1_Click event handler.

    Visual Basic .NET Code
    Private Sub ToolBar1_HandleCreated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolBar1.HandleCreated
         'The event is raised when the control is created.        
         MessageBox.Show("Handle Created")
    End Sub
    Private Sub ToolBar1_HandleDestroyed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolBar1.HandleDestroyed
         'The event is raised when the handle to the control is destroyed.
         MessageBox.Show("Handle Destroyed")
    End Sub
    Visual C# .NET Code
    private void toolBar1_HandleCreated(object sender, System.EventArgs e)
    {
         //The event is raised when the control is created.        
         MessageBox.Show ("Handle Created");
    }
    private void toolBar1_HandleDestroyed(object sender, System.EventArgs e)
    {
         //The event is raised when the handle of the control is destroyed.        
         MessageBox.Show ("Handle Destroyed");
    }
  14. On the Build menu, click Build Solution.
  15. On the Debug menu, click Start.
  16. On Form1, click Button1.

    The handle of ToolBar1 is destroyed, and then the control is re-created.

    Note The ToolBar control is re-created when you set the ToolBar button properties, such as the ImageIndex property, the Visible property, and the Style property.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MajorLast Reviewed:2/2/2006
Keywords:kbvs2005doesnotapply kbvs2005swept kbButton kbCtrlCreate kbProperties kbToolbar kbCtrl kbControl kbprb KB820641 kbAudDeveloper