Microsoft KB Archive/190226

= How To Programmatically Add Child Controls to a CoolBar =

Article ID: 190226

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q190226



SUMMARY
When adding a Child control to a CoolBar Band, you must specify the container argument in the Controls.Add method call, and then set the Child property of a Band to the new control.



MORE INFORMATION
The Child property of a CoolBar Band must be a control that is contained by the CoolBar. Therefore, while the Container argument of the Add method is optional, it must be supplied in order to add a Child control to a Band of a CoolBar control.

The Add method of the Controls Collection has these parts:

object.Add(ProgID, name, container)

Adding a control in this way does not make the control automatically appear because it is not yet a Child of a Band. Therefore, after you add the control, set the Child property of the appropriate Band to the new control. The control now appears. Note that a Band can only have one Child, so attempting to set the Child property of a Band that already has a Child control produces an error. See the REFERENCES section of this article for more information.

Step By Step Example
 Start a new Standard EXE project. Form1 is created by default. Select Components from the Projects menu, select "Microsoft Windows Common Controls-3 6.0", and click OK. Place a CoolBar control onto Form1. It has three Bands by default. Add two CommandButtons to Form1.  Place the following code into the form's module: Private Sub Command1_Click Dim MyCtrl As Object ' Add a new CommandButton to the first Band. Set MyCtrl = Controls.Add("VB.CommandButton", "cmdTest", CoolBar1) MyCtrl.Caption = "Test Button" Set CoolBar1.Bands(1).Child = MyCtrl ' place on first Band ' Add a new TextBox to the second Band. Set MyCtrl = Controls.Add("VB.TextBox", "txtTest", CoolBar1) MyCtrl.Text = "Testing Text" Set CoolBar1.Bands(2).Child = MyCtrl ' place on second Band End Sub

Private Sub Command2_Click Dim MyCtrl As Object ' Add a new CheckBox to a new Band. Set MyCtrl = Controls.Add("VB.CheckBox", "ckTest", CoolBar1) MyCtrl.Caption = "Check Test" CoolBar1.Bands.Add Child:=MyCtrl, Visible:=True   ' add a Band CoolBar1.Bands(CoolBar1.Bands.Count).Width = 1000 ' expand it      End Sub

 Run the project and click Command1. You will see a new CommandButton appear in the first Band and a new TextBox appear in the second Band. Click Command2 and a fourth Band is added with a new CheckBox. Note that when you set the Width property of a Band through code, it is not larger than the minimum necessary to accommodate its Child control, but you can resize it with the Mouse.</li></ol>

<div class="references_section">