Microsoft KB Archive/820641

= The ToolBar control is recreated when you set the properties of the button on the ToolBar at runtime =

Article ID: 820641

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-





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
 Start Microsoft Visual Studio .NET. Create a new Windows application by using Microsoft Visual Basic .NET or Microsoft Visual C# .NET.

By default, Form1 is created. Add a ToolBar control to Form1. Right-click ToolBar1 on Form1, and then click Properties. In the Properties dialog box, click the Buttons property, and then click the ellipsis button (...). In the ToolBarButton Collection Editor dialog box, click Add.</li> Set the Style property of ToolBarButton1 to DropDownButton.</li> Set the Text property of ToolBarButton1 to UpdateButton.</li> Click OK.</li> Add a Button control to Form1.</li>  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 = &quot;ChangedText&quot; Visual C# .NET Code //Set the Text property of the ToolBarButton. toolBar1.Buttons[0].Text= &quot;Changed Text&quot;; </li>  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); </li>  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(&quot;Handle Created&quot;) 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(&quot;Handle Destroyed&quot;) 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 (&quot;Handle Created&quot;); } private void toolBar1_HandleDestroyed(object sender, System.EventArgs e) { //The event is raised when the handle of the control is destroyed. MessageBox.Show (&quot;Handle Destroyed&quot;); } </li> On the Build menu, click Build Solution.</li> On the Debug menu, click Start.</li> 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.</li></ol>

<div class="references_section">